Zero to Hero
article thumbnail
206. Reverse Linked List
Algorithm 2021. 5. 5. 21:18

Reverse Linked List - 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. Stack을 사용한 풀이 class Solution: def reverseList(self, head: ListNode) -> ListNode: if not head: return None stack = [] cur = head while cur: stack.append(cur) cur = cur.next new_head = stack.pop() cur = new_head..

article thumbnail
234. Palindrome Linked List
Algorithm 2021. 5. 4. 14:36

Palindrome Linked List - 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. 단순한 풀이 class Solution: def isPalindrome(self, head: ListNode) -> bool: cur = head trace = [] while cur: trace.append(cur.val) cur = cur.next return trace == trace[::-1] 경로를 저장하고 경로와 뒤집은 경로가 같은지를 반환한다. 2. Po..

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..