forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsora0319.java
More file actions
44 lines (36 loc) · 1.05 KB
/
sora0319.java
File metadata and controls
44 lines (36 loc) · 1.05 KB
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
class Solution {
public int uniquePaths(int m, int n) {
int[][] pathN = new int[m][n];
boolean[][] visited = new boolean[m][n];
pathN[0][0] = 1;
bfs(pathN, visited);
return pathN[m-1][n-1];
}
public void bfs(int[][] pathN, boolean[][] visited){
Queue<Pair> paths = new LinkedList<>();
int[] mx = {0, 1};
int[] my = {1, 0};
paths.offer(new Pair(0,0));
while(!paths.isEmpty()){
Pair p = paths.poll();
for(int i = 0; i < 2; i++){
int nx = mx[i] + p.x;
int ny = my[i] + p.y;
if(nx >= pathN.length || ny >= pathN[0].length) continue;
pathN[nx][ny] += pathN[p.x][p.y];
if(!visited[nx][ny]){
paths.offer(new Pair(nx, ny));
visited[nx][ny] = true;
}
}
}
}
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
}