Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2b6c4d

Browse files
committed
question 62,63
1 parent 3d9a283 commit e2b6c4d

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,4 +2575,78 @@ public class Solution {
25752575
return head;
25762576
}
25772577
}
2578+
```
2579+
2580+
## 62. Unique Paths
2581+
题目:一个`mxn`的空间内,求左上角到右下角的路线共有多少条。
2582+
![route](https://leetcode.com/static/images/problemset/robot_maze.png)
2583+
2584+
一道很简单的动态规划题,当前的路线数
2585+
> a[m][n] = a[m-1][n]+a[m][n-1]
2586+
2587+
```java
2588+
public class Solution {
2589+
public int uniquePaths(int m, int n) {
2590+
if (m == 0 || n == 0) return 0;
2591+
if (m == 1 || n == 1) return 1;
2592+
int[][] a = new int[m][n];
2593+
Arrays.fill(a[0], 1);
2594+
for (int i = 1; i < m; i++) {
2595+
for (int j = 0; j < n; j++) {
2596+
if (j == 0) {
2597+
a[i][j] = 1;
2598+
} else {
2599+
a[i][j] = a[i - 1][j] + a[i][j - 1];
2600+
}
2601+
}
2602+
}
2603+
return a[m - 1][n - 1];
2604+
}
2605+
}
2606+
```
2607+
2608+
## 63. Unique Paths II
2609+
题目:题意跟上一题一样,只不过在mxn的空间里面加上障碍物妨碍路线。
2610+
2611+
思路跟上一题一样,只不过碰到障碍物时设置当前的位置的可到达路线为0
2612+
2613+
```java
2614+
public class Solution {
2615+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
2616+
int m = obstacleGrid.length;
2617+
if (m == 0) return 0;
2618+
int n = obstacleGrid[0].length;
2619+
if (n == 0) return 0;
2620+
int a[][] = new int[m][n];
2621+
if (obstacleGrid[0][0] > 0) {
2622+
a[0][0] = 0;
2623+
} else {
2624+
a[0][0] = 1;
2625+
}
2626+
for (int i = 1; i < n; i++) {
2627+
if (obstacleGrid[0][i] > 0) {
2628+
a[0][i] = 0;
2629+
} else {
2630+
a[0][i] = a[0][i - 1];
2631+
}
2632+
}
2633+
for (int i = 1; i < m; i++) {
2634+
if (obstacleGrid[i][0] > 0) {
2635+
a[i][0] = 0;
2636+
} else {
2637+
a[i][0] = a[i - 1][0];
2638+
}
2639+
}
2640+
for (int i = 1; i < m; i++) {
2641+
for (int j = 1; j < n; j++) {
2642+
if (obstacleGrid[i][j] > 0) {
2643+
a[i][j] = 0;
2644+
} else {
2645+
a[i][j] = a[i - 1][j] + a[i][j - 1];
2646+
}
2647+
}
2648+
}
2649+
return a[m - 1][n - 1];
2650+
}
2651+
}
25782652
```

src/UniquePaths.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* Created by cpacm on 2017/7/3.
5+
*/
6+
public class UniquePaths {
7+
public static void main(String[] args) {
8+
System.out.println(uniquePaths(4, 5));
9+
}
10+
11+
public static int uniquePaths(int m, int n) {
12+
if (m == 0 || n == 0) return 0;
13+
if (m == 1 || n == 1) return 1;
14+
int[][] a = new int[m][n];
15+
Arrays.fill(a[0], 1);
16+
for (int i = 1; i < m; i++) {
17+
for (int j = 0; j < n; j++) {
18+
if (j == 0) {
19+
a[i][j] = 1;
20+
} else {
21+
a[i][j] = a[i - 1][j] + a[i][j - 1];
22+
}
23+
}
24+
}
25+
return a[m - 1][n - 1];
26+
}
27+
}

src/UniquePathsII.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Created by cpacm on 2017/7/3.
3+
*/
4+
public class UniquePathsII {
5+
6+
public static void main(String[] args) {
7+
System.out.println(uniquePathsWithObstacles(new int[][]{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}));
8+
}
9+
10+
public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
11+
int m = obstacleGrid.length;
12+
if (m == 0) return 0;
13+
int n = obstacleGrid[0].length;
14+
if (n == 0) return 0;
15+
int a[][] = new int[m][n];
16+
if (obstacleGrid[0][0] > 0) {
17+
a[0][0] = 0;
18+
} else {
19+
a[0][0] = 1;
20+
}
21+
for (int i = 1; i < n; i++) {
22+
if (obstacleGrid[0][i] > 0) {
23+
a[0][i] = 0;
24+
} else {
25+
a[0][i] = a[0][i - 1];
26+
}
27+
}
28+
for (int i = 1; i < m; i++) {
29+
if (obstacleGrid[i][0] > 0) {
30+
a[i][0] = 0;
31+
} else {
32+
a[i][0] = a[i - 1][0];
33+
}
34+
}
35+
for (int i = 1; i < m; i++) {
36+
for (int j = 1; j < n; j++) {
37+
if (obstacleGrid[i][j] > 0) {
38+
a[i][j] = 0;
39+
} else {
40+
a[i][j] = a[i - 1][j] + a[i][j - 1];
41+
}
42+
}
43+
}
44+
return a[m - 1][n - 1];
45+
46+
}
47+
}

0 commit comments

Comments
 (0)