diff --git a/longest-substring-without-repeating-characters/hu6r1s.py b/longest-substring-without-repeating-characters/hu6r1s.py new file mode 100644 index 000000000..2cf72c135 --- /dev/null +++ b/longest-substring-without-repeating-characters/hu6r1s.py @@ -0,0 +1,17 @@ +class Solution: + """ + a => 세트 {}에 a 없음 + ab => 세트 {a}에 b 없음 + aba => 세트 {a, b}에 a 있음 => 중복 + abac => 더 이상 고려 가치 없음 + """ + def lengthOfLongestSubstring(self, s: str) -> int: + max_len = 0 + for start in range(len(s)): + chars = set() + for end in range(start, len(s)): + if s[end] in chars: + break + chars.add(s[end]) + max_len = max(end - start + 1, max_len) + return max_len diff --git a/reverse-linked-list/hu6r1s.py b/reverse-linked-list/hu6r1s.py new file mode 100644 index 000000000..6fc37bb45 --- /dev/null +++ b/reverse-linked-list/hu6r1s.py @@ -0,0 +1,24 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + """ + 링크드 리스트 학습을 따로 해야할 필요성이 있음 + """ + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + stack = [] + node = head + while node: + stack.append(node) + node = node.next + + dummy = ListNode() + output = dummy + while stack: + output.next = stack.pop() + output = output.next + + output.next = None + return dummy.next diff --git a/set-matrix-zeroes/hu6r1s.py b/set-matrix-zeroes/hu6r1s.py new file mode 100644 index 000000000..c58e4e84e --- /dev/null +++ b/set-matrix-zeroes/hu6r1s.py @@ -0,0 +1,16 @@ +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + zero = [] + for i in range(len(matrix)): + for j in range(len(matrix[0])): + if matrix[i][j] == 0: + zero.append((i, j)) + + for x, y in zero: + for i in range(len(matrix[0])): + matrix[x][i] = 0 + for i in range(len(matrix)): + matrix[i][y] = 0 diff --git a/unique-paths/hu6r1s.py b/unique-paths/hu6r1s.py new file mode 100644 index 000000000..77c268ecd --- /dev/null +++ b/unique-paths/hu6r1s.py @@ -0,0 +1,23 @@ +class Solution: + """ + dfs로 풀면 될 것이라고 생각은 했지만 이전 문제와 같은 방식일 줄 알았는데 이러한 방식이 있는 것을 알았음 + """ + # def uniquePaths(self, m: int, n: int) -> int: + # def dfs(x, y): + # if x == m - 1 and y == n - 1: + # return 1 + # if x >= m or y >= n: + # return 0 + + # return dfs(x + 1, y) + dfs(x, y + 1) + + # return dfs(0, 0) + + def uniquePaths(self, m: int, n: int) -> int: + dp = [[1] * n for _ in range(m)] + + for row in range(1, m): + for col in range(1, n): + dp[row][col] = dp[row - 1][col] + dp[row][col - 1] + + return dp[-1][-1]