Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must contain the digits 1-9 without repetition.Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.Note:A Sudoku board (partially filled) could be valid but is not necess..
array&hashing
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.You must write an algorithm that runs in O(n) time.longest consecutive sequence๋ฅผ ์ฐพ๋ ๋ฌธ์ ! O(n) ์์ผ๋ก ํ์ด์ผ ํ๋ค.์ฃผ์ํ ์ ์ constraints์ nums์ length๊ฐ 0๋ถํฐ ๊ฐ๋ฅํ๋ค๋ ๊ฒ Hash Set์ ์ด์ฉํ ํ์ดclass Solution: def longestConsecutive(self, nums: List[int]) -> int: numSet = set(nums) longest = 0 f..
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.You must write an algorithm that runs in O(n) time and without using the division operation.division ์ฐ์ฐ ์์ด O(n)์ ์๊ฐ ๋ณต์ก๋๋ก ๊ณ์ฐํ๋ผ๊ณ ์ ์ฝ ์กฐ๊ฑด์ด ์ฃผ์ด์ง ๋ฌธ์ ์๋ค! ํ์ดclass Solution: def produ..
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.๋น๋์๊ฐ ๋์ ์์๋๋ก, ์์ k๊ฐ์ elements๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์ ๋ค. ํ์ด 1class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = defaultdict(int) for i in nums: count[i] += 1 result = sorted(count, key=count.get, reverse=Tr..
anagram์ ์ผ๋จ sort ํ ๋น๊ต๋ฅผ ๋ ์ฌ๋ฆฌ์! ํ์ด1: ๋ฆฌ์คํธ ํ์ฉclass Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: result = [] visited = [False] * len(strs) for i in range(len(strs)): if visited[i]: continue tmp = [] tmp.append(strs[i]) visited[i] = True for j in range(len(strs)): if so..
๋ด ํ์ด: ๋ธ๋ฃจํธํฌ์คclass Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: result = set() for i in range(len(nums)): for j in range(len(nums)): if nums[i] + nums[j] == target and i != j: result.add(i) result.add(j) result = list(result) return sorted(result)set์๋ค๊ฐ ์ถ๊ฐํ๊ณ ๋ฆฌ์คํธ๋ก ๋ฐ๊พธ๊ณ sor..
ํ์ด1: ๋ฆฌ์คํธ ์ฌ์ฉclass Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False letters = [] for i in s: letters.append(i) for i in t: if i in letters: letters.remove(i) else: return False return True๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋จ์ํ๊ฒ ๊ตฌํํ ํ์ด ํ์ด2: sorted ํ ..
ํ์ด 1 : ๋ฆฌ์คํธ์ in ๋ฌธ๋ฒ ์ฌ์ฉclass Solution: def hasDuplicate(self, nums: List[int]) -> bool: count = [] for i in nums: if i not in count: count.append(i) else: return True return FalseNeetCode์์ ํต๊ณผํ ์ฝ๋๋ค.ํ์ง๋ง LeetCode์์๋ nums ๋ฐฐ์ด์ด ์์ฃผ ํฐ ํ
์คํธ์ผ์ด์ค์์ ์๊ฐ ์ด๊ณผ๋ก ํต๊ณผํ์ง ๋ชปํ๋ค. ํ์ด 2 : set๊ณผ in ๋ฌธ๋ฒ ์ฌ์ฉclass Solution: def containsDuplicate(se..