초보

Solution) def summaryRanges(self, nums: List[int]) -> List[str]: if not nums: return [] res = [] l, r = 0, 0 while r " + str(nums[r])) l = r+1 r += 1 if l == r: res.append(str(nums[r])) else: res.append(str(nums[l]) + "->" + str(nums[r])) return res Problem: LeetCode Time Complexity: ..
Solution)class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: window = {} window[nums[0]] = 0 l, r = 0, 1 while r < len(nums) and l < r: if r-l bool: window = {} window[nums[0]] = 0 l, r = 0, 1Expanding Window)We'll expand the window until the maximum of r-l becomes k. While expanding the window, if we're lucky enough to find a duplicate inside the window, we'll im..
Solution)class Solution: def wordPattern(self, pattern: str, s: str) -> bool: dic = {} s = s.split() if len(pattern) != len(s): return False for i in range(len(pattern)): if pattern[i] not in dic: dic[pattern[i]] = s[i] else: if dic[pattern[i]] != s[i]: return False dicCount = Counter(dic.values()) for counts in dicCount.values(): if counts > 1: return False return True Problem: LeetCode Time Co..
Solution) class Solution: def isIsomorphic(self, s: str, t: str) -> bool: dic = {} for i in range(len(s)): if s[i] not in dic: dic[s[i]] = t[i] else: if dic[s[i]] != t[i]: return False countDic = Counter(dic.values()) for count in countDic.values(): if count > 1: return False return True Problem: LeetCode Time Complexity: O(n) Space Complexity: O(n) Explanation) First, using a dictionary, set ch..
Solution)class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: magDic = {} for char in magazine: if char in magDic: magDic[char] += 1 else: magDic[char] = 1 for char in ransomNote: if char in magDic: magDic[char] -= 1 if magDic[char] == 0: del magDic[char] else: return False return True Problem: LeetCode Time Complexity: O(m+n) Space Complexity: O(m)Explanation)I think ..
Solution) class Solution: def isSubsequence(self, s: str, t: str) -> bool: original, sub = 0, 0 while sub < len(s) and original < len(t): if s[sub] == t[original]: sub += 1 original += 1 if sub == len(s): return True return False Problem: LeetCode Time Complexity: O(m+n) Space Complexity: O(1) Explanation) There are two pointers sub and original that will iterate s and t. We're using two pointer..
Solution) class Solution: def lengthOfLastWord(self, s: str) -> int: res = 0 for i in range(len(s)-1, -1, -1): if s[i].isalpha(): res += 1 else: if res: break return res Problem: LeetCode Time Complexity: O(n) Space Complexity: O(1) Explanation) We'll iterate through s backwards to find the last word. When we find the last word, we will count how many characters the last word has and return that..
Solution) class Solution: def removeElement(self, nums: List[int], val: int) -> int: res = 0 left, right = 0, len(nums)-1 while left int: res = 0 left, right = 0, len(nums)-1 while left
Solution) class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: fast, slow = head, head while fast and fast.next: fast = fast.next.next slow = slow.next prev = None while slow: temp = slow.next slow.next = prev prev = slow slow = temp left, right = head, prev while right: if left.val != right.val: return False left = left.next right = right.next return True Time Complexity: O..
정답) class Solution: def containsDuplicate(self, nums: List[int]) -> bool: dupSet = set() for n in nums: if n in dupSet: return True else: dupSet.add(n) return False 문제) 문제출처 정수 배열 nums가 주어졌을 때, 배열의 어떤 원소가 적어도 두 번 존재한다면 True를 리턴하시오. 만약 모든 원소가 한 번씩밖에 존재하지 않는다면 False를 리턴하시오. 예시 1) nums = [1, 2, 3, 1] output = True 예시 2) nums = [1, 2, 3, 4] output = False 예시 1) nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2] ..
위대한먼지
'초보' 태그의 글 목록 (7 Page)