File tree Expand file tree Collapse file tree 1 file changed +7
-9
lines changed Expand file tree Collapse file tree 1 file changed +7
-9
lines changed Original file line number Diff line number Diff line change @@ -176,16 +176,14 @@ class Solution {
176
176
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
177
177
int n = obstacleGrid.length, m = obstacleGrid[0].length;
178
178
int[][] dp = new int[n][m];
179
- dp[0][0] = 1 - obstacleGrid[0][0];
180
- for (int i = 1; i < m; i++) {
181
- if (obstacleGrid[0][i] == 0 && dp[0][i - 1] == 1) {
182
- dp[0][i] = 1;
183
- }
179
+
180
+ for (int i = 0; i < m; i++) {
181
+ if (obstacleGrid[0][i] == 1) break; //一旦遇到障碍,后续都到不了
182
+ dp[0][i] = 1;
184
183
}
185
- for (int i = 1; i < n; i++) {
186
- if (obstacleGrid[i][0] == 0 && dp[i - 1][0] == 1) {
187
- dp[i][0] = 1;
188
- }
184
+ for (int i = 0; i < n; i++) {
185
+ if (obstacleGrid[i][0] == 1) break; ////一旦遇到障碍,后续都到不了
186
+ dp[i][0] = 1;
189
187
}
190
188
for (int i = 1; i < n; i++) {
191
189
for (int j = 1; j < m; j++) {
You can’t perform that action at this time.
0 commit comments