Counting Bits - 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. 내장 함수를 이용한 탐색 def countBits(self, num: int) -> List[int]: answer = [] for i in range(num + 1): answer.append(format(i, "b").count("1")) return answer 2. 비트의 성질을 이용한 반복 사용 def countBits(self, num: int) -> List[int]: ..
Minimum Height 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. Brute Force DFS (시간 초과) from typing import * from collections import defaultdict class Solution: def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]: graph = defaultdict(set) for edge in..
Find the Duplicate 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 findDuplicate(self, nums): nums.sort() for i in range(1, len(nums)): if nums[i] == nums[i-1]: return nums[i] 2. HashMap or Set (공간 초과) class Solution: def findDuplicate(..
Product of Array Except Self - 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 productExceptSelf(self, nums: List[int]) -> List[int]: answer = [] temp1 = [1] for i in range(1, len(nums)): temp1.append(t..
Permutations - 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. 전형적인 DFS from typing import * from copy import deepcopy class Solution: def permute(self, nums: List[int]) -> List[List[int]]: answer = [] visited = [0] * len(nums) def sol(permu_list, visited_list): if len(permu_lis..
Binary Tree Inorder Traversal - 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. DFS를 이용한 Inorder 구현 from typing import * class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: answer = [] def move(cur): if cur.left: move(cur.left) answer.append(cur.val) if cur...