Zero to Hero
article thumbnail
21. Merge Two Sorted Lists
Algorithm 2021. 4. 26. 12:56

Merge Two Sorted 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. 내 Trash Garbage 코드 class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: zero_node = ListNode() cur = zero_node while l1 and l2: if l1.val >= l2.val: cur.next = l2 l2 = l2.next el..

article thumbnail
169. Majority Element
Algorithm 2021. 4. 23. 11:01

Majority Element - 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. 해시맵 스타일(Dict, Counter 사용) from collections import Counter class Solution: def majorityElement(self, nums: List[int]) -> int: counter = Counter() for num in nums: counter[num] += 1 return counter.most_common(1)[0]..

136. Single Number
Algorithm 2021. 4. 22. 14:31

Single Number - 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 singleNumber(self, nums: List[int]) -> int: set1 = set() for num in nums: if num not in set1: set1.add(num) else: set1.remove(num) return set1[0] 2. 조건의 특성 사용, 2*(a+b+c) - (2a+2b+c) = c cla..

article thumbnail
617. Merge Two Binary Trees
Algorithm 2021. 4. 21. 12:36

Merge Two Binary Trees - 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. 나의 Trash Garbage 코드 class Solution: def search(self, p1, p2): p1.val += (p2.val if p2 else 0) if p1.left: if p2: self.search(p1.left, p2.left) else: self.search(p1.left, TreeNode(0, None, None)) else: if p2..

14. Longest Common Prefix
Algorithm 2021. 4. 20. 16:42

Longest Common Prefix - 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. 완전 탐색 from typing import * class Solution: def sol(self, strs, length): if length == 0: return True for strr in strs: # length 보다 문자열이 짧은 상황은 불가능하니깐 반환 if len(strr) < length: return False if strs[0][:length]..

article thumbnail
424. Longest Repeating Character Replacement
Algorithm 2021. 4. 19. 14:03

Longest Repeating Character Replacement - 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 (실패) from typing import * from collections import defaultdict # 주어진 문자열에서 지정된 횟수만큼 알파벳을 교환했을 때 # 동일한 문자로 구성된 부분 문자열의 길이의 최댓값을 반환하시오 class Solution: def characterReplacement(self, s: str, ..