Skip to content

Commit af3a3bd

Browse files
committed
📝 update alg
1 parent 1f8902c commit af3a3bd

6 files changed

Lines changed: 184 additions & 0 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

Java/.DS_Store

0 Bytes
Binary file not shown.

Java/alg/.DS_Store

0 Bytes
Binary file not shown.

Java/alg/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@
119119
- [47.全排列2](lc/47.全排列2.md)
120120
- [55.跳跃游戏](lc/55.跳跃游戏.md)
121121
- [56.合并区间](lc/56.合并区间.md)
122+
- [62.不同路径](lc/62.不同路径.md)
123+
- [63.不同路径2](lc/63.不同路径2.md)
122124

123125
## 笔试
124126

Java/alg/lc/62.不同路径.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 62. 不同路径
2+
3+
[url](https://leetcode-cn.com/problems/unique-paths/)
4+
5+
## 题目
6+
7+
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
8+
9+
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
10+
11+
问总共有多少条不同的路径?
12+
13+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
14+
15+
```
16+
输入:m = 3, n = 7
17+
输出:28
18+
输入:m = 3, n = 2
19+
输出:3
20+
解释:
21+
从左上角开始,总共有 3 条路径可以到达右下角。
22+
1. 向右 -> 向下 -> 向下
23+
2. 向下 -> 向下 -> 向右
24+
3. 向下 -> 向右 -> 向下
25+
输入:m = 7, n = 3
26+
输出:28
27+
```
28+
29+
## 方法
30+
31+
## code
32+
33+
### js
34+
35+
```js
36+
let uniquePaths = (m, n) => {
37+
let dp = Array(n).fill(1);
38+
for (let i = 1; i < m; i++) {
39+
for (let j = 1; j < n; j++) {
40+
dp[j] += dp[j - 1];
41+
}
42+
}
43+
return dp[n - 1];
44+
}
45+
```
46+
47+
### go
48+
49+
```go
50+
func uniquePaths(m, n int) int {
51+
dp := make([]int, n)
52+
for i := 0; i < n; i++ {
53+
dp[i] = 1
54+
}
55+
for i := 1; i < m; i++ {
56+
for j := 1; j < n; j++ {
57+
dp[j] += dp[j - 1]
58+
}
59+
}
60+
return dp[n-1]
61+
}
62+
```
63+
64+
65+
### java
66+
67+
```java
68+
class Solution {
69+
public int uniquePaths(int m, int n) {
70+
int[] dp = new int[n];
71+
Arrays.fill(dp, 1);
72+
for (int i = 1; i < m; i++) {
73+
for (int j = 1; j < n; j++) {
74+
dp[j] += dp[j - 1];
75+
}
76+
}
77+
return dp[n - 1];
78+
}
79+
}
80+
```
81+

Java/alg/lc/63.不同路径2.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 63. 不同路径 II
2+
3+
4+
[url](https://leetcode-cn.com/problems/unique-paths-ii/)
5+
6+
## 题目
7+
8+
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
9+
10+
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
11+
12+
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
13+
14+
15+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/22/robot_maze.png)
16+
17+
18+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
19+
```
20+
输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
21+
输出:2
22+
解释:
23+
3x3 网格的正中间有一个障碍物。
24+
从左上角到右下角一共有 2 条不同的路径:
25+
1. 向右 -> 向右 -> 向下 -> 向下
26+
2. 向下 -> 向下 -> 向右 -> 向右
27+
```
28+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
29+
```
30+
输入:obstacleGrid = [[0,1],[0,0]]
31+
输出:1
32+
```
33+
34+
## 方法
35+
36+
## code
37+
38+
### js
39+
40+
```js
41+
let uniquePathsWithObstacles = obstacleGrid => {
42+
let m = obstacleGrid.length, n = obstacleGrid[0].length;
43+
let dp = Array(n + 1).fill(0);
44+
dp[1] = 1;
45+
for (let i = 1; i <= m; i ++) {
46+
for (let j = 1; j <= n; j++) {
47+
if (obstacleGrid[i - 1][j - 1] === 1) {
48+
dp[j] = 0;
49+
} else {
50+
dp[j] += dp[j - 1];
51+
}
52+
}
53+
}
54+
return dp[n];
55+
}
56+
```
57+
58+
### go
59+
60+
```go
61+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
62+
m, n := len(obstacleGrid), len(obstacleGrid[0])
63+
dp := make([]int, n + 1)
64+
dp[1] = 1
65+
for i := 1; i <= m; i++ {
66+
for j := 1; j <= n; j++ {
67+
if obstacleGrid[i-1][j-1] == 1 {
68+
dp[j] = 0
69+
} else {
70+
dp[j] += dp[j - 1]
71+
}
72+
}
73+
}
74+
return dp[n]
75+
}
76+
```
77+
78+
79+
### java
80+
81+
```java
82+
class Solution {
83+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
84+
int m = obstacleGrid.length;
85+
int n = obstacleGrid[0].length;
86+
int[] dp = new int[n + 1];
87+
dp[1] = 1;
88+
for (int i = 1; i <= m; i++){
89+
for (int j = 1; j <= n; j++) {
90+
if (obstacleGrid[i - 1][j - 1] == 1) {
91+
dp[j] = 0;
92+
} else {
93+
dp[j] += dp[j - 1];
94+
}
95+
}
96+
}
97+
return dp[n];
98+
}
99+
}
100+
```
101+

0 commit comments

Comments
 (0)