Game of Life - 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 * ''' Any live cell with fewer than two live neighbors dies as if caused by under-population. 8방으로 이웃이 2개 미만이면 죽인다 Any live cell with two or three live neighbors lives on to the next generation...
Number of 1 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. Bit 성질을 이용한 풀이 class Solution: def hammingWeight(self, n: int) -> int: answer = 0 while n: n = n & (n - 1) answer += 1 return answer 비트 조작은 굉장히 재밌는 성질들이 많다. 내가 아는 몇 안 되는 비트 조작 중 하나가 이것인데, 양의 정수 a와 a-1을 AND 연산하면 양..
Convert Sorted Array to Binary Search 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. DFS class Solution: def sortedArrayToBST(self, nums: List[int]) -> TreeNode: def dfs(cur_list): if not cur_list: return None mid = len(cur_list) // 2 cur_node = TreeNode(cur_list[mid]) c..
Longest Increasing Subsequence - 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 bisect import bisect_left class Solution: def lengthOfLIS(self, nums: List[int]) -> int: stack = [] for num in nums: if not stack: stack.append(num) else:..
Implement Trie (Prefix 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. Trie 자료구조 구현 class Trie: def __init__(self): self.head = Node() def insert(self, word: str) -> None: cur = self.head for char in word: if char not in cur.child: new_node = Node(char) cur.child[char] =..
Search a 2D Matrix II - 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 class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: temp = [] for item in matrix: temp += item if target in temp: return True return False 그냥 이렇게 배열을 정말 탐색해도 통과는..