[LeetCode/Python] 125. Valid Palindrome
·
Algorithm/LeetCode
문제 링크:https://leetcode.com/problems/valid-palindrome/description/?envType=study-plan-v2&envId=top-interview-150Palindrome 에 관한 문제이다. 문제만 잘 읽는다면 문제 없이 풀 수 있다.사실 leetcode를 풀 때 초반 장벽은 아마 영어 단어이지 않을까 싶다. 다행히 이 문제에서는 palindrome이 어떤 특징을 가진 단어인지 설명을 해주는데, 코테 문제에서는 설명을 안해주는 경우도 허다해서 난감했던 기억이 있다. [내 풀이]class Solution: def isPalindrome(self, s: str) -> bool: if len(s) == 0: return Tru..
[LeetCode/Python] 58. Length of Last Word
·
Algorithm/LeetCode
다시 알고리즘 공부를 꾸준히 하기 위한 작심삼일 x 1년! 프로젝트를 시작했다. (작심삼일이라도 꾸준히 하자라는 마음가짐이다) 문제 링크:https://leetcode.com/problems/length-of-last-word/description/?envType=study-plan-v2&envId=top-interview-150 Easy 난이도인 만큼, 특히 python으로서는 아주 풀기 쉬운 문제였다. [내 풀이]class Solution: def lengthOfLastWord(self, s: str) -> int: word_list = s.strip().split() return len(word_list[-1]) 시간 복잡도where n is the length of ..
[LeetCode/Python] 9. Palindrome Number
·
Algorithm/LeetCode
문제 링크: https://leetcode.com/problems/palindrome-number/?envType=study-plan-v2&envId=top-interview-150 숫자를 문자열로 바꿔서 풀면 아주 쉬운 문제다.근데 문제 말미에 문자열로 안바꾸고 풀어보라는 챌린지가 있길래 시도했다가 처참히 실패했다..ㅠ 일단 인풋을 문자열로 바꾸지 않고 숫자 자체로 두고 푼 코드는 아래와 같다.class Solution: def isPalindrome(self, x: int) -> bool: if x 0: n = len(str(x)) divisor = 10 **(n-1) left = x // divisor ..
[LeetCode/Python] 67. Add Binary
·
Algorithm/LeetCode
문제 링크: https://leetcode.com/problems/add-binary/description/?envType=study-plan-v2&envId=top-interview-150 숫자로 쉽게 치환해서 패키지를 써서 이진법 덧셈을 할 생각이었는데, string 자체로 풀어보고 싶다고 생각했다.  [내 풀이]class Solution: def addBinary(self, a: str, b: str) -> str: # without converting string to int carry = 0 i, j = len(a) - 1, len(b) - 1 result = [] while i >= 0 or j >= 0 or carry: ..
[NeetCode/Python] Depth of Binary Tree
·
카테고리 없음
문제: https://neetcode.io/problems/depth-of-binary-tree NeetCode neetcode.io [내 풀이]# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightresult = 0class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: global result # 뭐지.. 연속으로 test case 처리하면서 ..
[LeetCode/Python] 383. Ransome Note
·
카테고리 없음
문제 링크: https://leetcode.com/problems/ransom-note/description/?envType=study-plan-v2&envId=top-interview-150 [풀이 코드]class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: # letter -> int book = {} for letter in magazine: if letter in book: book[letter] += 1 else: book[letter] = 1 fo..