분류 전체보기 (73) 썸네일형 리스트형 [Top Interview 150 - Math] 9. Palindrome Number [Q] [Easy]Given an integer x, return true if x is a palindrome, and false otherwise. int -> list 바꾸는 과정이 필요했다.음수인 경우와 0보다 크고 10보다 작은 일의 자리 숫자인 경우도 따로 처리해줬다.제출하다보니 예외가 계속해서 나와서 그걸 처리하다 보니 코드가 좀 지저분해졌다.# Answerimport mathclass Solution: def isPalindrome(self, x: int) -> bool: # 음수 따로 처리 if x = 0) & (x 1) & (x_list[:middle-1] == list(reversed(x_list[middle:]))): .. [Top Interview 150 - Hashmap] 128. Longest Consecutive Sequence [Q] [Medium]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. 이 문제는 오름차순으로 하면 바로 풀릴 문제였지만, time complexity 로 인해 사용하기 어렵다.hash set 을 사용해야 한다.# Answerclass Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: return 0 num_set = set(nums) .. [Top Interview 150 - Hashmap] 219. Contains Duplicate II [Q] [Easy]Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) 문제가 참 이해가 안갔던 문제..# Answerclass Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: num_dict = {} for i, num in enumerate(nums): if num in num_dict and i - num_dict[num] [Top Interview 150 - Hashmap] 49. Group Anagrams [Q] [Medium]Given an array of strings strs, group the anagrams together. You can return the answer in any order. 일단 문제가 짧아서 좋았다 22..collections.defaultdict 함수 쓰면 아주 간단하게 해결되는데 참고한 문제 해결 방법이 독특해서 재밌었다.# Answerimport collectionsclass Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: anagrams = collections.defaultdict(list) for word in strs: anagrams.. [Top Interview 150 - Hashmap] 242. Valid Anagram [Q] [Medium]Given two strings s and t, return true if t is an anagram of s, and false otherwise. 일단 문제가 짧아서 좋았다.문제가 짧으니까 잘 풀리는 것 같기도..?count(i) 함수 써서 해당 '문자열' 의 개수를 value 로 넣어주니 쉽게 풀렸다.# Answerclass Solution: def isAnagram(self, s: str, t: str) -> bool: # 길이 안 맞으면 False if len(s) != len(t): return False s_dict = dict() t_dict = dict() for i in s: .. [Top Interview 150 - Hashmap] 290. Word Pattern [Q] [Easy]Given a pattern and a string s, find if s follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. Specifically:Each letter in pattern maps to exactly one unique word in s.Each unique word in s maps to exactly one letter in pattern.No two letters map to the same word, and no two words map to the same .. [Top Interview 150 - Hashmap] 205. Isomorphic Strings [Q] [Easy]Given two strings s and t, determine if they are isomorphic.Two strings s and t are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself. 길이 다르면 애초에 아니니까 따로 빼줬다.str.index() 함수 활용하여 index.. [Top Interview 150 - Hashmap] 383. Ransom Note [Q] [Easy]Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.Each letter in magazine can only be used once in ransomNote. Hash 문제는 익숙해지는 것이 중요한 것 같다.dict.get() 만 알면 수월하게 풀 수 있었던 문제# Answerclass Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: char_frequency = dict() .. 이전 1 2 3 4 5 6 ··· 10 다음