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 dc1269e

Browse files
committed
[03/18] 實際03/07, 63-uniquePathsWithObstacles
1 parent 71f332f commit dc1269e

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package algo
2+
3+
4+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
5+
// init
6+
metrix := make([][]int, len(obstacleGrid))
7+
for i:=0;i<len(obstacleGrid);i++{
8+
row := make([]int, len(obstacleGrid[0]))
9+
row[0] = 1
10+
metrix[i] = row
11+
}
12+
13+
for i:=0;i<len(obstacleGrid[0]);i++{
14+
metrix[0][i] = 1
15+
}
16+
if obstacleGrid[0][0] == 1{
17+
metrix[0][0] = 0
18+
} else{
19+
metrix[0][0] = 1
20+
}
21+
// 判斷
22+
for i:=1;i<len(obstacleGrid);i++{
23+
if obstacleGrid[i][0] == 1 {
24+
metrix[i][0] = 0
25+
}else{
26+
metrix[i][0] = metrix[i-1][0]
27+
}
28+
}
29+
for j:=1;j<len(obstacleGrid[0]);j++{
30+
if obstacleGrid[0][j] == 1 {
31+
metrix[0][j] = 0
32+
} else{
33+
metrix[0][j] = metrix[0][j-1]
34+
}
35+
}
36+
for i:=1;i<len(obstacleGrid);i++{
37+
for j:=1;j<len(obstacleGrid[0]);j++{
38+
if obstacleGrid[i][j] == 1 {
39+
metrix[i][j] = 0
40+
} else{
41+
metrix[i][j] = metrix[i-1][j] + metrix[i][j-1]
42+
}
43+
}
44+
}
45+
return metrix[len(obstacleGrid)-1][len(obstacleGrid[0])-1]
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package algo
2+
import "testing"
3+
4+
func Test(t *testing.T){
5+
if {
6+
t.Log("Success")
7+
} else{
8+
t.Error("Fail")
9+
}
10+
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module algo
2+
3+
go 1.17

0 commit comments

Comments
(0)

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