array&hashing

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..
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..
yesolz
'array&hashing' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก