Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a281dc

Browse files
committed
update 980.unique-paths-iii.java
1 parent 1f146c9 commit 7a281dc

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

‎980.unique-paths-iii.java‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,17 @@
7777
class Solution {
7878
int zeros = 0;
7979
private int callme(int x, int y, int[][] grid){
80-
if(x<0 || y<0 || x>=grid.length || y>=grid[0].length)
80+
if(x<0 || y<0 || x>=grid.length || y>=grid[0].length || grid[x][y] == -1 || grid[x][y] == 3)
8181
return 0;
82-
if(grid[x][y] == -1 || grid[x][y] == 3)
83-
return 0;
84-
if(grid[x][y] == 2){
85-
if(zeros == 0)
86-
return 1;
87-
else
88-
return 0;
89-
}
9082

91-
zeros--;
92-
grid[x][y] = 3;
83+
if(grid[x][y] == 2)
84+
return zeros == 0 ? 1 : 0;
85+
9386
int ret = 0;
9487

88+
grid[x][y] = 3;
89+
zeros--;
90+
9591
//left
9692
ret += callme(x, y-1, grid);
9793
//right
@@ -103,23 +99,24 @@ private int callme(int x, int y, int[][] grid){
10399

104100
zeros++;
105101
grid[x][y] = 0;
102+
106103
return ret;
107104
}
108105

109106
public int uniquePathsIII(int[][] grid) {
110107

108+
int x = 0, y = 0;
111109
for(int i=0; i<grid.length; i++)
112110
for(int j=0; j<grid[0].length; j++)
113111
if(grid[i][j] == 0)
114112
zeros++;
115-
for(int i=0; i<grid.length; i++)
116-
for(int j=0; j<grid[0].length; j++)
117-
if(grid[i][j] == 1){
118-
grid[i][j] = -1;
119-
return callme(i+1, j, grid)+callme(i-1, j, grid)+callme(i, j-1, grid)+callme(i, j+1, grid);
113+
else if(grid[i][j] == 1){
114+
x = i;
115+
y = j;
120116
}
121117

122-
return 0;
118+
grid[x][y] = -1;
119+
return callme(x+1, y, grid)+callme(x-1, y, grid)+callme(x, y-1, grid)+callme(x, y+1, grid);
123120
}
124121
}
125122
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /