Zero to Hero
article thumbnail
226. Invert Binary Tree
Algorithm 2021. 5. 3. 11:14

Invert Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Queue를 이용한 풀이 from collections import deque class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return None if not root.left and not root.right: return root q = deque([]) q.append(roo..

article thumbnail
141. Linked List Cycle
Algorithm 2021. 5. 3. 10:19

Linked List Cycle - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Object id값과 집합을 이용한 풀이 class Solution: def hasCycle(self, head: ListNode) -> bool: id_set = set() cur = head while cur: if id(cur) not in id_set: id_set.add(id(cur)) else: return True cur = cur.next return False..

53. Maximum Subarray
Algorithm 2021. 5. 2. 23:05

Maximum Subarray - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. prefix sum을 이용한 풀이 class Solution: def maxSubArray(self, nums: List[int]) -> int: if len(nums) == 1: return sum(nums) prefix = [nums[0]] for index, num in enumerate(nums): if index == 0: continue prefix.append(nu..

article thumbnail
101. Symmetric Tree
Algorithm 2021. 5. 2. 00:37

Symmetric Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. in-order 접근(실패) class Solution: def isSymmetric(self, root: TreeNode) -> bool: trace = [] def dfs(cur): if not cur: trace.append(None) else: if not (not cur.left and not cur.right): dfs(cur.left) trace.append(cur.v..

article thumbnail
155. Min Stack
Algorithm 2021. 4. 30. 17:04

Min Stack - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. event를 flag로 해서 처리하는 풀이 from heapq import * class MinStack: def __init__(self): self.stack = [] self.min_stack = [] self.events = [0] * 30000 self.event_cnt = 0 def push(self, val: int) -> None: self.stack.append((val, ..

article thumbnail
160. Intersection of Two Linked Lists
Algorithm 2021. 4. 30. 13:52

Intersection of Two Linked Lists - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 객체 ID값을 이용한 분기점 반환 class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: a_id_list = [] b_id_list = [] p_a, p_b = headA, headB while p_a: a_id_list.append(id..