Skip to content

Commit be17893

Browse files
Merge pull request #1453 from printjin-gmailcom/main
[printjin-gmailcom] Week 7 Solutions
2 parents 9e1ac18 + d0d3502 commit be17893

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s):
3+
char_set = set()
4+
left = 0
5+
max_len = 0
6+
7+
for right in range(len(s)):
8+
while s[right] in char_set:
9+
char_set.remove(s[left])
10+
left += 1
11+
char_set.add(s[right])
12+
max_len = max(max_len, right - left + 1)
13+
14+
return max_len
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def numIslands(self, grid):
3+
if not grid:
4+
return 0
5+
6+
rows, cols = len(grid), len(grid[0])
7+
count = 0
8+
9+
def dfs(r, c):
10+
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == "0":
11+
return
12+
grid[r][c] = "0"
13+
dfs(r+1, c)
14+
dfs(r-1, c)
15+
dfs(r, c+1)
16+
dfs(r, c-1)
17+
18+
for r in range(rows):
19+
for c in range(cols):
20+
if grid[r][c] == "1":
21+
dfs(r, c)
22+
count += 1
23+
24+
return count
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
7+
class Solution:
8+
def reverseList(self, head):
9+
prev = None
10+
current = head
11+
while current:
12+
nxt = current.next
13+
current.next = prev
14+
prev = current
15+
current = nxt
16+
return prev
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def setZeroes(self, matrix):
3+
first_row_zero = any(matrix[0][j] == 0 for j in range(len(matrix[0])))
4+
first_col_zero = any(matrix[i][0] == 0 for i in range(len(matrix)))
5+
6+
for i in range(1, len(matrix)):
7+
for j in range(1, len(matrix[0])):
8+
if matrix[i][j] == 0:
9+
matrix[i][0] = 0
10+
matrix[0][j] = 0
11+
12+
for i in range(1, len(matrix)):
13+
for j in range(1, len(matrix[0])):
14+
if matrix[i][0] == 0 or matrix[0][j] == 0:
15+
matrix[i][j] = 0
16+
17+
if first_row_zero:
18+
for j in range(len(matrix[0])):
19+
matrix[0][j] = 0
20+
21+
if first_col_zero:
22+
for i in range(len(matrix)):
23+
matrix[i][0] = 0

unique-paths/printjin-gmailcom.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def uniquePaths(self, m, n):
3+
dp = [[1] * n for _ in range(m)]
4+
for i in range(1, m):
5+
for j in range(1, n):
6+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
7+
return dp[m - 1][n - 1]

0 commit comments

Comments
 (0)