코-딩/Leetcode

코-딩/Leetcode

[Leetcode] 219. Contains Duplicate II

문제 : https://leetcode.com/problems/contains-duplicate-ii/description/ 난이도 : Easy 풀이 class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: nDict = {} for i, n in enumerate(nums): if n in nDict and abs(i - nDict[n])

코-딩/Leetcode

[leetcode] 205. Isomorphic Strings

문제 : https://leetcode.com/problems/isomorphic-strings/description/ 난이도 : Easy 풀이 class Solution: def isIsomorphic(self, s: str, t: str) -> bool: sDict = [] tDict = [] for idx in s: sDict.append(s.index(idx)) for idx in t: tDict.append(t.index(idx)) if sDict == tDict: return True return False 처음 나타내는 문자의 인덱스를 찾아주는 index 함수를 사용해서 각 단어들의 인덱스를 저장해준다. 그리고 그 인덱스 사전들끼리 비교해주면 된다!

코-딩/Leetcode

[leetcode] 79. Word Search

문제 : https://leetcode.com/problems/word-search/ 난이도 : Medium 풀이 class Solution: def exist(self, board: List[List[str]], word: str) -> bool: if not word: return True m, n = len(board), len(board[0]) if len(word) > m * n: return False counter = Counter(word) for line in board: for c in line: if c in counter: counter[c] -= 1 for v in counter.values(): if v > 0: return False def DFS(r, c, w): if not..

코-딩/Leetcode

[leetcode] 79. Word Search

문제 : https://leetcode.com/problems/word-search/description/ 난이도 : Medium 풀이 class Solution: def exist(self, board: List[List[str]], word: str) -> bool: M, N = len(board), len(board[0]) def dfs(x, y, d): if x >= M or x = N or y < 0 or word[d] != board[x][y]: return False if d == len(word)-1: return True temp = board[x][y] board[x][y] = '0' # 방문여부 found = dfs(x,y-1,d+1) or dfs(x,y+1,d+1)..

코-딩/Leetcode

[leetcode] 435. Non-overlapping Intervals

문제 : https://leetcode.com/problems/non-overlapping-intervals/description/ 난이도 : Medium 인터벌들이 겹치지 않도록 제거해야하는 인터벌 수 구하는 문제!! 풀이 class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: count = 0 intervals.sort() for i in range(len(intervals) - 1): if intervals[i + 1][0] < intervals[i][1]: count += 1 intervals[i + 1][1] = min(intervals[i + 1][1],intervals[i][1]) return co..

코-딩/Leetcode

[leetcode] 1342. Number of Steps to Reduce a Number to Zero

문제 : https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/description/ 난이도 : Easy 풀이 class Solution: def numberOfSteps(self, num: int) -> int: cnt = 0 while num > 0: if num % 2 == 0: num /= 2 else: num -= 1 cnt += 1 return cnt 이건 뭐... 너무 쉬워서 할 말이 없움ㅎ 다른 풀이 class Solution: def numberOfSteps(self, num: int) -> int: return bin(num).count("1") * 2 + bin(num).count("0") - 2 한 줄로 끝..

힞뚜루마뚜루
'코-딩/Leetcode' 카테고리의 글 목록 (2 Page)