Leetcode (32) 썸네일형 리스트형 Leetcode 20. Valid Parentheses 월요일 아침을 여는 리트코드! 오늘은 쉬운 문제로 선택했다. (연구가 시급해서... 논문도 덜읽었어...) Valid parentheses이다. class Solution: def isValid(self, s: str) -> bool: parentheses = {')': '(', '}': '{', ']' : '['} stack = [] for c in s: if c in parentheses.values(): stack.append(c) if c in parentheses.keys(): if stack == []: return False elif parentheses[c] != stack.pop(): return False return stack == [] 코드는 stack을 이용하는데, 가령 {가 마지.. Leetcode 122. Best Time to Buy and Sell Stock II 오늘은 주식 최고의 이윤을 구하는 문제이다. prices = [a, b, c]라 할 때, a b, a int: profit = 0 for i in range(len(prices) - 1): if prices[i] < prices[i + 1]: profit += (prices[i + 1] - prices[i]) return profit 간단하다! 쉽게 끝이 난다. runtime과 memor.. Leetcode 118. Pascal's Triangle 서울의 아침에 하는 리트코드... 쉬운걸로 했다... class Solution: def generate(self, numRows: int) -> List[List[int]]: ans = [[1]] for i in range(1, numRows): row = [1] for j in range(i-1): row.append(ans[i-1][j] + ans[i-1][j+1]) row.append(1) ans.append(row) return ans 끝! 441. Arranging Coins 1부터 k까지의 합은 n > k(k+1) / 2 를 만족하는 k의 최댓값를 찾는 문제이다. 전개하면 다음과 같이 되는데, 중학생 때 많이 사용한 근의공식을 이용하면, 으로 표현이 된다. 이 때 가장 큰 정수 k값을 찾으면 되므로, int로 씌워주면 끝! import math class Solution: def arrangeCoins(self, n: int) -> int: return int( (-1 + math.sqrt(1 + 8 * n)) / 2) 수학의 중요성 하하 오늘도 끝! 서울가서 신나게 놀아야지 129. Sum Root to Leaf Numbers 오늘도...힘차게...퐈이팅... 오늘은 Root에서 Leaf까지 한 번 거칠 때 마다 10씩 곱해져서 더해진 숫자를 모두 더하는 (?) 문제이다. DFS를 사용하여 해결하였다. # 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 = right class Solution: def sumNumbers(self, root: Optional[TreeNode]) -> int: def dfs(root, res): if not root: return 0 res = res * 10 + root.. 232. Implement Queue using Stacks 오늘 리트코드 문제는 스택을 이용해 큐를 구현하는 것이다. class MyQueue: def __init__(self): self.queue = [] def push(self, x: int) -> None: self.queue.append(x) def pop(self) -> int: return self.queue.pop(0) def peek(self) -> int: return self.queue[0] def empty(self) -> bool: if self.queue: return False return True # Your MyQueue object will be instantiated and called as such: # obj = MyQueue() # obj.push(x) # param_2 .. 83. Remove Duplicates from Sorted List 토요일 아침도 활기차게(?) 리트코드와 함께! # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: cur = head while(cur): while cur.next and cur.val == cur.next.val: cur.next = cur.next.next cur = cur.next return head 오늘은 바쁘니(? 사실 안바쁨) 여기까지만 바잉 Leetcode 226. Invert Binary Tree 오늘은 시험이 끝났다 ! 야호 그리고 서류 합격 발표가 났다. 열심히 면접 준비 해야지 히히 오늘은 경북대학교 벤치에 앉아서 코드를 짰다. 간단한 문제여서 기분이 좋다. # 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 = right class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return root self.invertTree(root.left) s.. 이전 1 2 3 4 다음