분류 전체보기267 [LeetCode] Lowest Common Ancestor 파이썬 (tree) class Solution: # @param {TreeNode} root # @param {TreeNode} p # @param {TreeNode} q # @return {TreeNode} def lowestCommonAncestor(self, root, p, q): s, b = sorted([p.val, q.val]) while not s 2021. 4. 7. [LeetCode] Palindrome Linked List 파이썬 class Solution: # @param {ListNode} head # @return {boolean} def isPalindrome(self, head): # get mid point cur = head N = 0 while cur: N += 1 cur = cur.next mid = N//2 i = 0 def reverse(head): ans = None while head: nx = head.next head.next = ans ans = head head = nx return ans first = second = head # to mid point while i < mid: second = second.next i += 1 second = reverse(second) # check palind.. 2021. 4. 6. [LeetCode] Summary Ranges 파이썬 class Solution: # @param {integer[]} nums # @return {string[]} def summaryRanges(self, nums): if not nums: return [] start = nums[0] end = nums[0] ans = [] def beautify(a, b): if a == b: return str(a) else: return str(a) + "->" + str(b) for i in range(1,len(nums)): if (nums[i] - nums[i-1]) != 1: ans.append(beautify(start,nums[i-1])) start = nums[i] ans.append(beautify(start, nums[-1])) return an.. 2021. 4. 6. [LeetCode] Invert Binary Tree 파이썬(recursion) recursively # 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: TreeNode) -> TreeNode: def recu(node): if node == None: return recu(node.left) recu(node.right) node.left, node.right = node.right, node.left recu(root) return root iteratively cl.. 2021. 4. 5. [LeetCode] Contains Duplicate II 파이썬 (dict) class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: seen = {} for idx, value in enumerate(nums): if value not in seen: seen[value] = [idx] else: seen[value] += [abs(idx - seen[value][-1])] for key in seen.keys(): if len(seen[key])>1: if sum( [x 0: return True return False seen 이라는 dict를 만들어서 value 들은 list 형식으로 추가해서 duplicate일 경우(seen에 있는 key일 경우) 현재 위치 - 기존의 가장 최근.. 2021. 4. 5. [LeetCode] Reverse Linked List 파이썬 iterative way # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: ListNode) -> ListNode: prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev recursive way class Solution: # @param {ListNode} head # @return {ListNode} def re.. 2021. 4. 5. [LeetCode] Isomorphic Strings 파이썬 class Solution: def isIsomorphic(self, s: str, t: str) -> bool: s2t, t2s = {}, {} for p, w in zip(s, t): if w not in s2t and p not in t2s: s2t[w] = p t2s[p] = w elif w not in s2t or s2t[w] != p: # Contradict mapping. return False return True 서로 상대방의 string을 key와 value로 map 한다. elif 문 w(p의 char)가 s의 dict인 s2t에 없거나(처음 보는 다른 char일 경우) s2t의 key는 있지만 (t의 char이 s에 있지만, 즉 기존에 mapped 되었지만), 새로운 char가 등장.. 2021. 4. 5. [LeetCode] Count Primes 파이썬 class Solution: def countPrimes(self, n: int) -> int: return len(self.getPrimaryNum_Eratos(n)) def getPrimaryNum_Eratos(self,N): nums = [True] * (N + 1) for i in range(2, len(nums) // 2 + 1): if nums[i] == True: for j in range(i+i, N, i): nums[j] = False return [i for i in range(2, N) if nums[i] == True] 출처: somjang.tistory.com/entry/leetCode-204-Count-Primes-Python 2021. 4. 4. [LeetCode] Remove Linked List Elements 파이썬 (linked list) class Solution: # @param {ListNode} head # @param {integer} val # @return {ListNode} def removeElements(self, head, val): dummy = ListNode(float("-inf")) dummy.next = head prev, curr = dummy, dummy.next while curr: if curr.val == val: prev.next = curr.next else: prev = curr curr = curr.next return dummy.next dummy를 만들고 dummy의 카피인 prev, curr 2개를 만들어서 문제해결 dummy 하나만 쓰고 하는 풀이 class Solution: def re.. 2021. 4. 4. 이전 1 ··· 7 8 9 10 11 12 13 ··· 30 다음