Movatterモバイル変換


[0]ホーム

URL:


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

Commit42e5a34

Browse files
add a solution for 417
1 parentbad47f7 commit42e5a34

File tree

1 file changed

+63
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+63
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_417.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
packagecom.fishercoder.solutions;
22

33
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.LinkedList;
46
importjava.util.List;
7+
importjava.util.Queue;
58

69
publicclass_417 {
710
publicstaticclassSolution1 {
@@ -57,4 +60,64 @@ void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) {
5760
}
5861
}
5962

63+
publicstaticclassSolution2 {
64+
publicList<List<Integer>>pacificAtlantic(int[][]matrix) {
65+
List<List<Integer>>result =newArrayList<>();
66+
if (matrix ==null ||matrix.length ==0 ||matrix[0].length ==0) {
67+
returnresult;
68+
}
69+
intm =matrix.length;
70+
intn =matrix[0].length;
71+
for (inti =0;i <m;i++) {
72+
for (intj =0;j <n;j++) {
73+
boolean[]touchesPacificAndAtlantic =newboolean[2];
74+
update(i,j,m,n,touchesPacificAndAtlantic);
75+
Queue<int[]>queue =newLinkedList<>();
76+
boolean[][]visited =newboolean[m][n];
77+
visited[i][j] =true;
78+
queue.offer(newint[]{i,j});
79+
if (bfs(matrix,m,n,touchesPacificAndAtlantic,queue,visited)) {
80+
result.add(newArrayList<>(Arrays.asList(i,j)));
81+
}
82+
}
83+
}
84+
returnresult;
85+
}
86+
87+
privatevoidupdate(inti,intj,intm,intn,boolean[]touchesPacificAndAtlantic) {
88+
if (i ==0 ||j ==0) {
89+
touchesPacificAndAtlantic[0] =true;
90+
}
91+
if (i ==m -1 ||j ==n -1) {
92+
touchesPacificAndAtlantic[1] =true;
93+
}
94+
}
95+
96+
privatebooleanbfs(int[][]matrix,intm,intn,boolean[]touchesPacificAndAtlantic,Queue<int[]>queue,boolean[][]visited) {
97+
if (touchesPacificAndAtlantic[0] &&touchesPacificAndAtlantic[1]) {
98+
returntrue;
99+
}
100+
int[]directions =newint[]{0,1,0, -1,0};
101+
while (!queue.isEmpty()) {
102+
intsize =queue.size();
103+
for (intk =0;k <size;k++) {
104+
int[]curr =queue.poll();
105+
for (intp =0;p <directions.length -1;p++) {
106+
intnewx =curr[0] +directions[p];
107+
intnewy =curr[1] +directions[p +1];
108+
if (newx >=0 &&newx <m &&newy >=0 &&newy <n &&matrix[newx][newy] <=matrix[curr[0]][curr[1]] && !visited[newx][newy]) {
109+
visited[newx][newy] =true;
110+
queue.offer(newint[]{newx,newy});
111+
update(newx,newy,m,n,touchesPacificAndAtlantic);
112+
if (touchesPacificAndAtlantic[0] &&touchesPacificAndAtlantic[1]) {
113+
returntrue;
114+
}
115+
}
116+
}
117+
}
118+
}
119+
returnfalse;
120+
}
121+
}
122+
60123
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp