Skip to content

Commit 5c67548

Browse files
committed
feat:add golang solution for leetcode promblem 0063
1 parent cc68428 commit 5c67548

File tree

3 files changed

+131
-29
lines changed

3 files changed

+131
-29
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,32 @@
3535
## 解法
3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
### Go
39+
``` go
40+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
41+
m,n := len(obstacleGrid),len(obstacleGrid[0])
42+
dp := make([][]int,m)
43+
for i:=0; i < m;i++ {
44+
dp[i] = make([]int,n)
45+
}
46+
for i := 0; i < m; i++ {
47+
for j := 0; j < n; j++ {
48+
if obstacleGrid[i][j] == 0 {
49+
if i == 0 && j == 0 {
50+
dp[i][j] = 1
51+
} else if i > 0 && j >0 {
52+
dp[i][j] = dp[i][j-1]+dp[i-1][j]
53+
} else if i > 0 {
54+
dp[i][j] = dp[i-1][j]
55+
} else {
56+
dp[i][j] = dp[i][j-1]
57+
}
58+
}
59+
}
60+
}
61+
return dp[m-1][n-1]
62+
}
63+
3864

3965
### Python3
4066
<!-- 这里可写当前语言的特殊实现逻辑 -->

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

+82-29
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,93 @@
11
# [63. Unique Paths II](https://leetcode.com/problems/unique-paths-ii)
22

33
## Description
4-
<p>A robot is located at the top-left corner of a <em>m</em> x <em>n</em> grid (marked &#39;Start&#39; in the diagram below).</p>
5-
6-
<p>The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked &#39;Finish&#39; in the diagram below).</p>
7-
8-
<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?</p>
9-
10-
<p><img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;" /></p>
11-
12-
<p>An obstacle and empty space is marked as <code>1</code> and <code>0</code> respectively in the grid.</p>
13-
14-
<p><strong>Note:</strong> <em>m</em> and <em>n</em> will be at most 100.</p>
15-
16-
<p><strong>Example 1:</strong></p>
17-
18-
<pre>
19-
<strong>Input:
20-
</strong>[
21-
&nbsp; [0,0,0],
22-
&nbsp; [0,1,0],
23-
&nbsp; [0,0,0]
24-
]
25-
<strong>Output:</strong> 2
26-
<strong>Explanation:</strong>
27-
There is one obstacle in the middle of the 3x3 grid above.
28-
There are two ways to reach the bottom-right corner:
29-
1. Right -&gt; Right -&gt; Down -&gt; Down
30-
2. Down -&gt; Down -&gt; Right -&gt; Right
31-
</pre>
4+
<p>A robot is located at the top-left corner of a <em>m</em> x <em>n</em> grid (marked &#39;Start&#39; in the diagram below).</p>
325

336

347

35-
## Solutions
8+
<p>The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked &#39;Finish&#39; in the diagram below).</p>
9+
10+
11+
12+
<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?</p>
13+
14+
15+
16+
<p><img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;" /></p>
17+
18+
19+
20+
<p>An obstacle and empty space is marked as <code>1</code> and <code>0</code> respectively in the grid.</p>
21+
22+
23+
24+
<p><strong>Note:</strong> <em>m</em> and <em>n</em> will be at most 100.</p>
25+
26+
27+
28+
<p><strong>Example 1:</strong></p>
29+
30+
31+
32+
<pre>
3633

34+
<strong>Input:
3735

36+
</strong>[
37+
38+
&nbsp; [0,0,0],
39+
40+
&nbsp; [0,1,0],
41+
42+
&nbsp; [0,0,0]
43+
44+
]
45+
46+
<strong>Output:</strong> 2
47+
48+
<strong>Explanation:</strong>
49+
50+
There is one obstacle in the middle of the 3x3 grid above.
51+
52+
There are two ways to reach the bottom-right corner:
53+
54+
1. Right -&gt; Right -&gt; Down -&gt; Down
55+
56+
2. Down -&gt; Down -&gt; Right -&gt; Right
57+
58+
</pre>
59+
60+
61+
62+
63+
## Solutions
64+
65+
### Go
66+
```go
67+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
68+
m,n := len(obstacleGrid),len(obstacleGrid[0])
69+
dp := make([][]int,m)
70+
for i:=0; i < m;i++ {
71+
dp[i] = make([]int,n)
72+
}
73+
for i := 0; i < m; i++ {
74+
for j := 0; j < n; j++ {
75+
if obstacleGrid[i][j] == 0 {
76+
if i == 0 && j == 0 {
77+
dp[i][j] = 1
78+
} else if i > 0 && j >0 {
79+
dp[i][j] = dp[i][j-1]+dp[i-1][j]
80+
} else if i > 0 {
81+
dp[i][j] = dp[i-1][j]
82+
} else {
83+
dp[i][j] = dp[i][j-1]
84+
}
85+
}
86+
}
87+
}
88+
return dp[m-1][n-1]
89+
}
90+
```
3891
### Python3
3992

4093
```python
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
2+
m,n := len(obstacleGrid),len(obstacleGrid[0])
3+
dp := make([][]int,m)
4+
for i:=0; i < m;i++ {
5+
dp[i] = make([]int,n)
6+
}
7+
for i := 0; i < m; i++ {
8+
for j := 0; j < n; j++ {
9+
if obstacleGrid[i][j] == 0 {
10+
if i == 0 && j == 0 {
11+
dp[i][j] = 1
12+
} else if i > 0 && j >0 {
13+
dp[i][j] = dp[i][j-1]+dp[i-1][j]
14+
} else if i > 0 {
15+
dp[i][j] = dp[i-1][j]
16+
} else {
17+
dp[i][j] = dp[i][j-1]
18+
}
19+
}
20+
}
21+
}
22+
return dp[m-1][n-1]
23+
}

0 commit comments

Comments
 (0)