From 5c67548c1c6b845b2ae9b0cacedc2919fc2506e4 Mon Sep 17 00:00:00 2001 From: Bin Liang Date: Mon, 6 Jul 2020 23:04:58 +0800 Subject: [PATCH] feat:add golang solution for leetcode promblem 0063 --- .../0000-0099/0063.Unique Paths II/README.md | 26 ++++ .../0063.Unique Paths II/README_EN.md | 111 +++++++++++++----- .../0063.Unique Paths II/Solution.go | 23 ++++ 3 files changed, 131 insertions(+), 29 deletions(-) create mode 100644 solution/0000-0099/0063.Unique Paths II/Solution.go diff --git a/solution/0000-0099/0063.Unique Paths II/README.md b/solution/0000-0099/0063.Unique Paths II/README.md index f70ebc5fdd42a..ca00b790f1bb4 100644 --- a/solution/0000-0099/0063.Unique Paths II/README.md +++ b/solution/0000-0099/0063.Unique Paths II/README.md @@ -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 diff --git a/solution/0000-0099/0063.Unique Paths II/README_EN.md b/solution/0000-0099/0063.Unique Paths II/README_EN.md index 85f58bb59c840..782da82c65475 100644 --- a/solution/0000-0099/0063.Unique Paths II/README_EN.md +++ b/solution/0000-0099/0063.Unique Paths II/README_EN.md @@ -1,40 +1,93 @@ # [63. Unique Paths II](https://leetcode.com/problems/unique-paths-ii) ## Description -

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

- -

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 'Finish' in the diagram below).

- -

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

- -

- -

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

- -

Note: m and n will be at most 100.

- -

Example 1:

- -

-Input:

-[

-  [0,0,0],

-  [0,1,0],

-  [0,0,0]

-]

-Output: 2

-Explanation:

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

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

-1. Right -> Right -> Down -> Down

-2. Down -> Down -> Right -> Right

-
+

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

-## Solutions +

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 'Finish' in the diagram below).

+ + + +

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

+ + + +

+ + + +

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

+ + + +

Note: m and n will be at most 100.

+ + + +

Example 1:

+ + + +
 
+Input:
 
+[
+
+  [0,0,0],
+
+  [0,1,0],
+
+  [0,0,0]
+
+]
+
+Output: 2
+
+Explanation:
+
+There is one obstacle in the middle of the 3x3 grid above.
+
+There are two ways to reach the bottom-right corner:
+
+1. Right -> Right -> Down -> Down
+
+2. Down -> Down -> Right -> Right
+
+
+ + + + +## 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 diff --git a/solution/0000-0099/0063.Unique Paths II/Solution.go b/solution/0000-0099/0063.Unique Paths II/Solution.go new file mode 100644 index 0000000000000..1089ae4ef2099 --- /dev/null +++ b/solution/0000-0099/0063.Unique Paths II/Solution.go @@ -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] +} \ No newline at end of file