Skip to content

Commit 75cbd9c

Browse files
committed
0062. Unique Paths
1 parent 48345dd commit 75cbd9c

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed

markdown/0062. Unique Paths.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
### [62\. Unique Paths](https://leetcode.com/problems/unique-paths/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
A robot is located at the top-left corner of a _m_ x _n_ grid (marked 'Start' in the diagram below).
7+
8+
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).
9+
10+
How many possible unique paths are there?
11+
12+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
13+
<small style="display: inline;">Above is a 7 x 3 grid. How many possible unique paths are there?</small>
14+
15+
**Note:** _m_ and _n_ will be at most 100.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: m = 3, n = 2
21+
Output: 3
22+
Explanation:
23+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
24+
1\. Right -> Right -> Down
25+
2\. Right -> Down -> Right
26+
3\. Down -> Right -> Right
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: m = 7, n = 3
33+
Output: 28
34+
```
35+
36+
37+
#### Solution
38+
39+
Language: **Java**
40+
41+
```java
42+
class Solution {
43+
   public int uniquePaths(int m, int n) {
44+
       int[][] ways = new int[m + 1][n + 1]; // 所有的格子都能到达,因此初始化为0,表示没有计算过。该数组用于保存到达该坐标的路径个数,防止重复计算
45+
       return uniquePaths(ways, m, n);
46+
  }
47+
48+
   private int uniquePaths(int[][] ways, int m, int n) {
49+
       if (ways[m][n] > 0) {
50+
           return ways[m][n];
51+
      }
52+
       if (m * n == 0) {
53+
           ways[m][n] = 0;
54+
           return 0;
55+
      }
56+
       if (m == 1 || n == 1) {
57+
           ways[m][n] = 1;
58+
           return 1;
59+
      }
60+
       ways[m][n] = uniquePaths(ways, m, n - 1) + uniquePaths(ways, m - 1, n);
61+
       return ways[m][n];
62+
  }
63+
}
64+
```
65+
这里第一次提交超时了,由于没考虑重复计算的问题,造成如果棋盘很大的情况下,会导致重复计算问题明显。
66+
![](http://ww4.sinaimg.cn/large/006tNc79ly1g51045ecr4j31b40q643d.jpg)

src/main/java/leetcode/_61_/Main.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
public class Main {
77
public static void main(String[] args) {
88
Solution solution = new Solution();
9-
System.out.println( solution.isValid(""));
109
}
1110
}
1211

src/main/java/leetcode/_62_/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._62_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.uniquePaths(7, 3));
10+
System.out.println(solution.uniquePaths(51, 9));
11+
}
12+
}
13+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode._62_;
2+
3+
class Solution {
4+
public int uniquePaths(int m, int n) {
5+
int[][] ways = new int[m + 1][n + 1]; // 所有的格子都能到达,因此初始化为0,表示没有计算过。该数组用于保存到达该坐标的路径个数,防止重复计算
6+
return uniquePaths(ways, m, n);
7+
}
8+
9+
private int uniquePaths(int[][] ways, int m, int n) {
10+
if (ways[m][n] > 0) {
11+
return ways[m][n];
12+
}
13+
if (m * n == 0) {
14+
ways[m][n] = 0;
15+
return 0;
16+
}
17+
if (m == 1 || n == 1) {
18+
ways[m][n] = 1;
19+
return 1;
20+
}
21+
ways[m][n] = uniquePaths(ways, m, n - 1) + uniquePaths(ways, m - 1, n);
22+
return ways[m][n];
23+
}
24+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
### [62\. Unique Paths](https://leetcode.com/problems/unique-paths/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
A robot is located at the top-left corner of a _m_ x _n_ grid (marked 'Start' in the diagram below).
7+
8+
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).
9+
10+
How many possible unique paths are there?
11+
12+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
13+
<small style="display: inline;">Above is a 7 x 3 grid. How many possible unique paths are there?</small>
14+
15+
**Note:** _m_ and _n_ will be at most 100.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: m = 3, n = 2
21+
Output: 3
22+
Explanation:
23+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
24+
1\. Right -> Right -> Down
25+
2\. Right -> Down -> Right
26+
3\. Down -> Right -> Right
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: m = 7, n = 3
33+
Output: 28
34+
```
35+
36+
37+
#### Solution
38+
39+
Language: **Java**
40+
41+
```java
42+
class Solution {
43+
   public int uniquePaths(int m, int n) {
44+
       int[][] ways = new int[m + 1][n + 1]; // 所有的格子都能到达,因此初始化为0,表示没有计算过。该数组用于保存到达该坐标的路径个数,防止重复计算
45+
       return uniquePaths(ways, m, n);
46+
  }
47+
48+
   private int uniquePaths(int[][] ways, int m, int n) {
49+
       if (ways[m][n] > 0) {
50+
           return ways[m][n];
51+
      }
52+
       if (m * n == 0) {
53+
           ways[m][n] = 0;
54+
           return 0;
55+
      }
56+
       if (m == 1 || n == 1) {
57+
           ways[m][n] = 1;
58+
           return 1;
59+
      }
60+
       ways[m][n] = uniquePaths(ways, m, n - 1) + uniquePaths(ways, m - 1, n);
61+
       return ways[m][n];
62+
  }
63+
}
64+
```
65+
这里第一次提交超时了,由于没考虑重复计算的问题,造成如果棋盘很大的情况下,会导致重复计算问题明显。
66+
![](http://ww4.sinaimg.cn/large/006tNc79ly1g51045ecr4j31b40q643d.jpg)

0 commit comments

Comments
 (0)