Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add golang solution for leetcode promblem 0063 #275

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions solution/0000-0099/0063.Unique Paths II/README.md
Original file line number Diff line number Diff line change
@@ -35,6 +35,32 @@
## 解法
<!-- 这里可写通用的实现逻辑 -->

### Go
``` go
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
m,n := len(obstacleGrid),len(obstacleGrid[0])
dp := make([][]int,m)
for i:=0; i < m;i++ {
dp[i] = make([]int,n)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if obstacleGrid[i][j] == 0 {
if i == 0 && j == 0 {
dp[i][j] = 1
} else if i > 0 && j >0 {
dp[i][j] = dp[i][j-1]+dp[i-1][j]
} else if i > 0 {
dp[i][j] = dp[i-1][j]
} else {
dp[i][j] = dp[i][j-1]
}
}
}
}
return dp[m-1][n-1]
}


### Python3
<!-- 这里可写当前语言的特殊实现逻辑 -->
111 changes: 82 additions & 29 deletions solution/0000-0099/0063.Unique Paths II/README_EN.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,93 @@
# [63. Unique Paths II](https://leetcode.com/problems/unique-paths-ii)

## Description
<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>

<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>

<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?</p>

<p><img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;" /></p>

<p>An obstacle and empty space is marked as <code>1</code> and <code>0</code> respectively in the grid.</p>

<p><strong>Note:</strong> <em>m</em> and <em>n</em> will be at most 100.</p>

<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:
</strong>[
&nbsp; [0,0,0],
&nbsp; [0,1,0],
&nbsp; [0,0,0]
]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -&gt; Right -&gt; Down -&gt; Down
2. Down -&gt; Down -&gt; Right -&gt; Right
</pre>
<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>



## Solutions
<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>



<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?</p>



<p><img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;" /></p>



<p>An obstacle and empty space is marked as <code>1</code> and <code>0</code> respectively in the grid.</p>



<p><strong>Note:</strong> <em>m</em> and <em>n</em> will be at most 100.</p>



<p><strong>Example 1:</strong></p>



<pre>

<strong>Input:

</strong>[

&nbsp; [0,0,0],

&nbsp; [0,1,0],

&nbsp; [0,0,0]

]

<strong>Output:</strong> 2

<strong>Explanation:</strong>

There is one obstacle in the middle of the 3x3 grid above.

There are two ways to reach the bottom-right corner:

1. Right -&gt; Right -&gt; Down -&gt; Down

2. Down -&gt; Down -&gt; Right -&gt; Right

</pre>




## Solutions

### Go
```go
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
m,n := len(obstacleGrid),len(obstacleGrid[0])
dp := make([][]int,m)
for i:=0; i < m;i++ {
dp[i] = make([]int,n)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if obstacleGrid[i][j] == 0 {
if i == 0 && j == 0 {
dp[i][j] = 1
} else if i > 0 && j >0 {
dp[i][j] = dp[i][j-1]+dp[i-1][j]
} else if i > 0 {
dp[i][j] = dp[i-1][j]
} else {
dp[i][j] = dp[i][j-1]
}
}
}
}
return dp[m-1][n-1]
}
```
### Python3

```python
23 changes: 23 additions & 0 deletions solution/0000-0099/0063.Unique Paths II/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
m,n := len(obstacleGrid),len(obstacleGrid[0])
dp := make([][]int,m)
for i:=0; i < m;i++ {
dp[i] = make([]int,n)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if obstacleGrid[i][j] == 0 {
if i == 0 && j == 0 {
dp[i][j] = 1
} else if i > 0 && j >0 {
dp[i][j] = dp[i][j-1]+dp[i-1][j]
} else if i > 0 {
dp[i][j] = dp[i-1][j]
} else {
dp[i][j] = dp[i][j-1]
}
}
}
}
return dp[m-1][n-1]
}