-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib.rs
54 lines (54 loc) · 1.41 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode.cn id=63 lang=rust
*
* [63] 不同路径 II
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 {
let m = obstacle_grid.len();
let n = obstacle_grid[0].len();
let mut dp = vec![vec![1; n]; m];
// 特殊处理第一列的情况
let mut hasObstacle = false;
for i in 0..n {
if obstacle_grid[0][i] == 1 {
hasObstacle = true
}
if hasObstacle {
dp[0][i] = 0
}
}
hasObstacle = false;
// 特殊处理第一行的情况
for i in 0..m {
if obstacle_grid[i][0] == 1 {
hasObstacle = true
}
if hasObstacle {
dp[i][0] = 0
}
}
for i in 1..m {
for j in 1..n {
let top = if obstacle_grid[i - 1][j] == 1 {
0
} else {
dp[i - 1][j]
};
let left = if obstacle_grid[i][j - 1] == 1 {
0
} else {
dp[i][j - 1]
};
dp[i][j] = top + left;
if obstacle_grid[i][j] == 1 {
dp[i][j] = 0
}
}
}
dp[m - 1][n - 1]
}
}
// @lc code=end