[LeetCode]

Solution)class Solution: def isPalindrome(self, x: int) -> bool: # Base Case if x < 0: return False # Initialization rev_x = 0 original = x # Making a reversed x while x: rev_x = (rev_x * 10) + (x % 10) x //= 10 # Check if palindrome if rev_x == original: return True return False Problem: LeetCode Time Complexity: O(n) Space Complexity: O(1)Explanation)We'll make a reversed x integer and compare..
Solution) class Solution: def addBinary(self, a: str, b: str) -> str: # Initialization res = "" carry = 0 aIdx, bIdx = len(a)-1, len(b)-1 while aIdx >= 0 or bIdx >= 0: # Adding integers from a and b with carry total = carry if aIdx >= 0: total += int(a[aIdx]) if bIdx >= 0: total += int(b[bIdx]) # Updating carry carry = 0 if total >= 2: total -= 2 carry = 1 # Concatenating the sum to res res += s..
Solution)class Solution: def searchInsert(self, nums: List[int], target: int) -> int: # Two Pointers left, right = 0, len(nums)-1 while left target: # Reached the start of an array if center == 0: return center # Update right pointer right = center-1 else: # Reached the end of an array if center == len(nums)-1: return center+1 # Update left pointer left = center+1 Problem: LeetCode Time Complexi..
Solution) class Solution: def getMinimumDifference(self, root: Optional[TreeNode]) -> int: # Initialization prev, res = None, float("inf") # Depth-First-Search def dfs(node): # Base Case if not node: return # Adjusting the scope nonlocal prev, res # Left Subtree dfs(node.left) # Recursive Call if prev: res = min(res, node.val - prev.val) prev = node # Right Subtree dfs(node.right) dfs(root) retu..
Solution) class Solution: def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]: queue = deque([root]) res = [] while queue: nodeSum = 0 nodeCount = len(queue) for i in range(nodeCount): cur = queue.popleft() nodeSum += cur.val if cur.left: queue.append(cur.left) if cur.right: queue.append(cur.right) res.append(nodeSum/nodeCount) return res Problem: LeetCode Time Complexity: O(n) Sp..
Solution) class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return self.invertTree(root.left) self.invertTree(root.right) root.left, root.right = root.right, root.left return root Problem: LeetCode Reference: NeetCode Time Complexity: O(2^n) Space Complexity: O(1) Explanation) This recursive function will invert a current node's children. Base cas..
Solution) class Solution: def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: def helper(root1, root2): if not root1 and not root2: return True if root1 and not root2: return False if not root1 and root2: return False leftRes = helper(root1.left, root2.left) rightRes = helper(root1.right, root2.right) return root1.val == root2.val and leftRes and rightRes return helper(p,..
Solution) def summaryRanges(self, nums: List[int]) -> List[str]: if not nums: return [] res = [] l, r = 0, 0 while r " + str(nums[r])) l = r+1 r += 1 if l == r: res.append(str(nums[r])) else: res.append(str(nums[l]) + "->" + str(nums[r])) return res Problem: LeetCode Time Complexity: ..
Solution)class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: window = {} window[nums[0]] = 0 l, r = 0, 1 while r < len(nums) and l < r: if r-l bool: window = {} window[nums[0]] = 0 l, r = 0, 1Expanding Window)We'll expand the window until the maximum of r-l becomes k. While expanding the window, if we're lucky enough to find a duplicate inside the window, we'll im..
Solution)class Solution: def wordPattern(self, pattern: str, s: str) -> bool: dic = {} s = s.split() if len(pattern) != len(s): return False for i in range(len(pattern)): if pattern[i] not in dic: dic[pattern[i]] = s[i] else: if dic[pattern[i]] != s[i]: return False dicCount = Counter(dic.values()) for counts in dicCount.values(): if counts > 1: return False return True Problem: LeetCode Time Co..
위대한먼지
'[LeetCode]' 카테고리의 글 목록 (6 Page)