Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01150d1

Browse files
committed
feat: add solutions to lc problem: No.0063. Unique Paths II
1 parent dd93d74 commit 01150d1

File tree

13 files changed

+259
-122
lines changed

13 files changed

+259
-122
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
- [矩阵区域和](./solution/1300-1399/1314.Matrix%20Block%20Sum/README.md)
191191
- [二维区域和检索 - 矩阵不可变](./solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README.md)
192192
- [不同路径](./solution/0000-0099/0062.Unique%20Paths/README.md)
193+
- [不同路径 II](./solution/0000-0099/0063.Unique%20Paths%20II/README.md)
193194
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
194195
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)
195196
- [最长上升子序列](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)

‎README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
184184
- [Matrix Block Sum](./solution/1300-1399/1314.Matrix%20Block%20Sum/README_EN.md)
185185
- [Range Sum Query 2D - Immutable](./solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README_EN.md)
186186
- [Unique Paths](./solution/0000-0099/0062.Unique%20Paths/README_EN.md)
187+
- [Unique Paths II](./solution/0000-0099/0063.Unique%20Paths%20II/README_EN.md)
187188
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
188189
- [Longest Increasing Subsequence](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
189190
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)

‎solution/0000-0099/0062.Unique Paths/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Solution:
7575
for i in range(1, m):
7676
for j in range(1, n):
7777
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
78-
return dp[m -1][n -1]
78+
return dp[-1][-1]
7979
```
8080

8181
### **Java**

‎solution/0000-0099/0062.Unique Paths/README_EN.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ From the top-left corner, there are a total of 3 ways to reach the bottom-right
5454

5555
## Solutions
5656

57+
Dynamic programming.
58+
5759
<!-- tabs:start -->
5860

5961
### **Python3**
@@ -65,7 +67,7 @@ class Solution:
6567
for i in range(1, m):
6668
for j in range(1, n):
6769
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
68-
return dp[m -1][n -1]
70+
return dp[-1][-1]
6971
```
7072

7173
### **Java**

‎solution/0000-0099/0062.Unique Paths/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ def uniquePaths(self, m: int, n: int) -> int:
44
for i in range(1, m):
55
for j in range(1, n):
66
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
7-
return dp[m-1][n-1]
7+
return dp[-1][-1]

‎solution/0000-0099/0063.Unique Paths II/README.md‎

Lines changed: 96 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,54 +48,122 @@
4848
<li><code>obstacleGrid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>
4949
</ul>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
5554

56-
### **Go**
55+
动态规划。
5756

58-
````go
59-
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
60-
m,n := len(obstacleGrid),len(obstacleGrid[0])
61-
dp := make([][]int,m)
62-
for i:=0; i < m;i++ {
63-
dp[i] = make([]int,n)
64-
}
65-
for i := 0; i < m; i++ {
66-
for j := 0; j < n; j++ {
67-
if obstacleGrid[i][j] == 0 {
68-
if i == 0 && j == 0 {
69-
dp[i][j] = 1
70-
} else if i > 0 && j >0 {
71-
dp[i][j] = dp[i][j-1]+dp[i-1][j]
72-
} else if i > 0 {
73-
dp[i][j] = dp[i-1][j]
74-
} else {
75-
dp[i][j] = dp[i][j-1]
76-
}
77-
}
78-
}
79-
}
80-
return dp[m-1][n-1]
81-
}
57+
假设 `dp[i][j]` 表示到达网格 `(i,j)` 的路径数,先初始化 dp 第一列和第一行的所有值,然后判断。
8258

59+
-`obstacleGrid[i][j] == 1`,说明路径数为 0,`dp[i][j] = 0`;
60+
-`obstacleGrid[i][j] == 0`,则 `dp[i][j] = dp[i - 1][j] + dp[i][j - 1]`
61+
62+
最后返回 `dp[m - 1][n - 1]` 即可。
8363

8464
<!-- tabs:start -->
8565

8666
### **Python3**
67+
8768
<!-- 这里可写当前语言的特殊实现逻辑 -->
8869

8970
```python
90-
91-
````
71+
class Solution:
72+
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
73+
m, n = len(obstacleGrid), len(obstacleGrid[0])
74+
dp = [[0] * n for _ in range(m)]
75+
for i in range(m):
76+
if obstacleGrid[i][0] == 1:
77+
break
78+
dp[i][0] = 1
79+
for j in range(n):
80+
if obstacleGrid[0][j] == 1:
81+
break
82+
dp[0][j] = 1
83+
for i in range(1, m):
84+
for j in range(1, n):
85+
if obstacleGrid[i][j] == 0:
86+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
87+
return dp[-1][-1]
88+
```
9289

9390
### **Java**
9491

9592
<!-- 这里可写当前语言的特殊实现逻辑 -->
9693

9794
```java
95+
class Solution {
96+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
97+
int m = obstacleGrid.length, n = obstacleGrid[0].length;
98+
int[][] dp = new int[m][n];
99+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; ++i) {
100+
dp[i][0] = 1;
101+
}
102+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; ++j) {
103+
dp[0][j] = 1;
104+
}
105+
for (int i = 1; i < m; ++i) {
106+
for (int j = 1; j < n; ++j) {
107+
if (obstacleGrid[i][j] == 0) {
108+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
109+
}
110+
}
111+
}
112+
return dp[m - 1][n - 1];
113+
}
114+
}
115+
```
116+
117+
### **C++**
98118

119+
```cpp
120+
class Solution {
121+
public:
122+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
123+
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
124+
vector<vector<int>> dp(m, vector<int>(n));
125+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; ++i) {
126+
dp[i][0] = 1;
127+
}
128+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; ++j) {
129+
dp[0][j] = 1;
130+
}
131+
for (int i = 1; i < m; ++i) {
132+
for (int j = 1; j < n; ++j) {
133+
if (obstacleGrid[i][j] == 0) {
134+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
135+
}
136+
}
137+
}
138+
return dp[m - 1][n - 1];
139+
}
140+
};
141+
```
142+
143+
### **Go**
144+
145+
```go
146+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
147+
m, n := len(obstacleGrid), len(obstacleGrid[0])
148+
dp := make([][]int, m)
149+
for i := 0; i < m; i++ {
150+
dp[i] = make([]int, n)
151+
}
152+
for i := 0; i < m && obstacleGrid[i][0] == 0; i++ {
153+
dp[i][0] = 1
154+
}
155+
for j := 0; j < n && obstacleGrid[0][j] == 0; j++ {
156+
dp[0][j] = 1
157+
}
158+
for i := 1; i < m; i++ {
159+
for j := 1; j < n; j++ {
160+
if obstacleGrid[i][j] == 0 {
161+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
162+
}
163+
}
164+
}
165+
return dp[m-1][n-1]
166+
}
99167
```
100168

101169
### **...**

‎solution/0000-0099/0063.Unique Paths II/README_EN.md‎

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,46 +44,107 @@ There are two ways to reach the bottom-right corner:
4444

4545
## Solutions
4646

47-
### **Go**
48-
49-
```go
50-
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
51-
m,n := len(obstacleGrid),len(obstacleGrid[0])
52-
dp := make([][]int,m)
53-
for i:=0; i < m;i++ {
54-
dp[i] = make([]int,n)
55-
}
56-
for i := 0; i < m; i++ {
57-
for j := 0; j < n; j++ {
58-
if obstacleGrid[i][j] == 0 {
59-
if i == 0 && j == 0 {
60-
dp[i][j] = 1
61-
} else if i > 0 && j >0 {
62-
dp[i][j] = dp[i][j-1]+dp[i-1][j]
63-
} else if i > 0 {
64-
dp[i][j] = dp[i-1][j]
65-
} else {
66-
dp[i][j] = dp[i][j-1]
67-
}
68-
}
69-
}
70-
}
71-
return dp[m-1][n-1]
72-
}
73-
```
47+
Dynamic programming.
7448

7549
<!-- tabs:start -->
7650

7751
### **Python3**
7852

7953
```python
80-
54+
class Solution:
55+
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
56+
m, n = len(obstacleGrid), len(obstacleGrid[0])
57+
dp = [[0] * n for _ in range(m)]
58+
for i in range(m):
59+
if obstacleGrid[i][0] == 1:
60+
break
61+
dp[i][0] = 1
62+
for j in range(n):
63+
if obstacleGrid[0][j] == 1:
64+
break
65+
dp[0][j] = 1
66+
for i in range(1, m):
67+
for j in range(1, n):
68+
if obstacleGrid[i][j] == 0:
69+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
70+
return dp[-1][-1]
8171
```
8272

8373
### **Java**
8474

8575
```java
76+
class Solution {
77+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
78+
int m = obstacleGrid.length, n = obstacleGrid[0].length;
79+
int[][] dp = new int[m][n];
80+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; ++i) {
81+
dp[i][0] = 1;
82+
}
83+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; ++j) {
84+
dp[0][j] = 1;
85+
}
86+
for (int i = 1; i < m; ++i) {
87+
for (int j = 1; j < n; ++j) {
88+
if (obstacleGrid[i][j] == 0) {
89+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
90+
}
91+
}
92+
}
93+
return dp[m - 1][n - 1];
94+
}
95+
}
96+
```
8697

98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
104+
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
105+
vector<vector<int>> dp(m, vector<int>(n));
106+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; ++i) {
107+
dp[i][0] = 1;
108+
}
109+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; ++j) {
110+
dp[0][j] = 1;
111+
}
112+
for (int i = 1; i < m; ++i) {
113+
for (int j = 1; j < n; ++j) {
114+
if (obstacleGrid[i][j] == 0) {
115+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
116+
}
117+
}
118+
}
119+
return dp[m - 1][n - 1];
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
128+
m, n := len(obstacleGrid), len(obstacleGrid[0])
129+
dp := make([][]int, m)
130+
for i := 0; i < m; i++ {
131+
dp[i] = make([]int, n)
132+
}
133+
for i := 0; i < m && obstacleGrid[i][0] == 0; i++ {
134+
dp[i][0] = 1
135+
}
136+
for j := 0; j < n && obstacleGrid[0][j] == 0; j++ {
137+
dp[0][j] = 1
138+
}
139+
for i := 1; i < m; i++ {
140+
for j := 1; j < n; j++ {
141+
if obstacleGrid[i][j] == 0 {
142+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
143+
}
144+
}
145+
}
146+
return dp[m-1][n-1]
147+
}
87148
```
88149

89150
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
4+
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
5+
vector<vector<int>> dp(m, vector<int>(n));
6+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; ++i) {
7+
dp[i][0] = 1;
8+
}
9+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; ++j) {
10+
dp[0][j] = 1;
11+
}
12+
for (int i = 1; i < m; ++i) {
13+
for (int j = 1; j < n; ++j) {
14+
if (obstacleGrid[i][j] == 0) {
15+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
16+
}
17+
}
18+
}
19+
return dp[m - 1][n - 1];
20+
}
21+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /