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 @@ -179,16 +179,14 @@ class Solution {
179
179
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
180
180
int n = obstacleGrid.length, m = obstacleGrid[0].length;
181
181
int[][] dp = new int[n][m];
182
- dp[0][0] = 1 - obstacleGrid[0][0];
183
- for (int i = 1; i < m; i++) {
184
- if (obstacleGrid[0][i] == 0 && dp[0][i - 1] == 1) {
185
- dp[0][i] = 1;
186
- }
182
+
183
+ for (int i = 0; i < m; i++) {
184
+ if (obstacleGrid[0][i] == 1) break; //一旦遇到障碍,后续都到不了
185
+ dp[0][i] = 1;
187
186
}
188
- for (int i = 1; i < n; i++) {
189
- if (obstacleGrid[i][0] == 0 && dp[i - 1][0] == 1) {
190
- dp[i][0] = 1;
191
- }
187
+ for (int i = 0; i < n; i++) {
188
+ if (obstacleGrid[i][0] == 1) break; ////一旦遇到障碍,后续都到不了
189
+ dp[i][0] = 1;
192
190
}
193
191
for (int i = 1; i < n; i++) {
194
192
for (int j = 1; j < m; j++) {
You can’t perform that action at this time.
0 commit comments