diff --git a/longest-substring-without-repeating-characters/printjin-gmailcom.py b/longest-substring-without-repeating-characters/printjin-gmailcom.py new file mode 100644 index 000000000..e8cea37f7 --- /dev/null +++ b/longest-substring-without-repeating-characters/printjin-gmailcom.py @@ -0,0 +1,14 @@ +class Solution: + def lengthOfLongestSubstring(self, s): + char_set = set() + left = 0 + max_len = 0 + + for right in range(len(s)): + while s[right] in char_set: + char_set.remove(s[left]) + left += 1 + char_set.add(s[right]) + max_len = max(max_len, right - left + 1) + + return max_len diff --git a/number-of-islands/printjin-gmailcom.py b/number-of-islands/printjin-gmailcom.py new file mode 100644 index 000000000..ed64bc8a4 --- /dev/null +++ b/number-of-islands/printjin-gmailcom.py @@ -0,0 +1,24 @@ +class Solution: + def numIslands(self, grid): + if not grid: + return 0 + + rows, cols = len(grid), len(grid[0]) + count = 0 + + def dfs(r, c): + if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == "0": + return + grid[r][c] = "0" + dfs(r+1, c) + dfs(r-1, c) + dfs(r, c+1) + dfs(r, c-1) + + for r in range(rows): + for c in range(cols): + if grid[r][c] == "1": + dfs(r, c) + count += 1 + + return count diff --git a/reverse-linked-list/printjin-gmailcom.py b/reverse-linked-list/printjin-gmailcom.py new file mode 100644 index 000000000..749921884 --- /dev/null +++ b/reverse-linked-list/printjin-gmailcom.py @@ -0,0 +1,16 @@ +# 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): + prev = None + current = head + while current: + nxt = current.next + current.next = prev + prev = current + current = nxt + return prev diff --git a/set-matrix-zeroes/printjin-gmailcom.py b/set-matrix-zeroes/printjin-gmailcom.py new file mode 100644 index 000000000..41273d4f6 --- /dev/null +++ b/set-matrix-zeroes/printjin-gmailcom.py @@ -0,0 +1,23 @@ +class Solution: + def setZeroes(self, matrix): + first_row_zero = any(matrix[0][j] == 0 for j in range(len(matrix[0]))) + first_col_zero = any(matrix[i][0] == 0 for i in range(len(matrix))) + + for i in range(1, len(matrix)): + for j in range(1, len(matrix[0])): + if matrix[i][j] == 0: + matrix[i][0] = 0 + matrix[0][j] = 0 + + for i in range(1, len(matrix)): + for j in range(1, len(matrix[0])): + if matrix[i][0] == 0 or matrix[0][j] == 0: + matrix[i][j] = 0 + + if first_row_zero: + for j in range(len(matrix[0])): + matrix[0][j] = 0 + + if first_col_zero: + for i in range(len(matrix)): + matrix[i][0] = 0 diff --git a/unique-paths/printjin-gmailcom.py b/unique-paths/printjin-gmailcom.py new file mode 100644 index 000000000..46123174e --- /dev/null +++ b/unique-paths/printjin-gmailcom.py @@ -0,0 +1,7 @@ +class Solution: + def uniquePaths(self, m, n): + dp = [[1] * n for _ in range(m)] + for i in range(1, m): + for j in range(1, n): + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + return dp[m - 1][n - 1]