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

Commit96025bc

Browse files
refactor 407
1 parent708fe3e commit96025bc

File tree

1 file changed

+50
-48
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+50
-48
lines changed

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

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,65 @@
2727
2828
*/
2929
publicclass_407 {
30-
/**Reference: https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue*/
31-
publicclassCell {
32-
introw;
33-
intcol;
34-
intheight;
35-
36-
publicCell(introw,intcol,intheight) {
37-
this.row =row;
38-
this.col =col;
39-
this.height =height;
30+
publicstaticclassSolution1 {
31+
/** Reference: https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue */
32+
publicclassCell {
33+
introw;
34+
intcol;
35+
intheight;
36+
37+
publicCell(introw,intcol,intheight) {
38+
this.row =row;
39+
this.col =col;
40+
this.height =height;
41+
}
4042
}
41-
}
4243

43-
publicinttrapRainWater(int[][]heights) {
44-
if (heights ==null ||heights.length ==0 ||heights[0].length ==0) {
45-
return0;
46-
}
44+
publicinttrapRainWater(int[][]heights) {
45+
if (heights ==null ||heights.length ==0 ||heights[0].length ==0) {
46+
return0;
47+
}
4748

48-
PriorityQueue<Cell>queue =newPriorityQueue<>(1, (a,b) ->a.height -b.height);
49+
PriorityQueue<Cell>queue =newPriorityQueue<>(1, (a,b) ->a.height -b.height);
4950

50-
intm =heights.length;
51-
intn =heights[0].length;
52-
boolean[][]visited =newboolean[m][n];
51+
intm =heights.length;
52+
intn =heights[0].length;
53+
boolean[][]visited =newboolean[m][n];
5354

54-
// Initially, add all the Cells which are on borders to the queue.
55-
for (inti =0;i <m;i++) {
56-
visited[i][0] =true;
57-
visited[i][n -1] =true;
58-
queue.offer(newCell(i,0,heights[i][0]));
59-
queue.offer(newCell(i,n -1,heights[i][n -1]));
60-
}
55+
// Initially, add all the Cells which are on borders to the queue.
56+
for (inti =0;i <m;i++) {
57+
visited[i][0] =true;
58+
visited[i][n -1] =true;
59+
queue.offer(newCell(i,0,heights[i][0]));
60+
queue.offer(newCell(i,n -1,heights[i][n -1]));
61+
}
6162

62-
for (inti =0;i <n;i++) {
63-
visited[0][i] =true;
64-
visited[m -1][i] =true;
65-
queue.offer(newCell(0,i,heights[0][i]));
66-
queue.offer(newCell(m -1,i,heights[m -1][i]));
67-
}
63+
for (inti =0;i <n;i++) {
64+
visited[0][i] =true;
65+
visited[m -1][i] =true;
66+
queue.offer(newCell(0,i,heights[0][i]));
67+
queue.offer(newCell(m -1,i,heights[m -1][i]));
68+
}
6869

69-
// from the borders, pick the shortest cell visited and check its neighbors:
70-
// if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped
71-
// add all its neighbors to the queue.
72-
int[][]dirs =newint[][]{{-1,0}, {1,0}, {0, -1}, {0,1}};
73-
intres =0;
74-
while (!queue.isEmpty()) {
75-
Cellcell =queue.poll();
76-
for (int[]dir :dirs) {
77-
introw =cell.row +dir[0];
78-
intcol =cell.col +dir[1];
79-
if (row >=0 &&row <m &&col >=0 &&col <n && !visited[row][col]) {
80-
visited[row][col] =true;
81-
res +=Math.max(0,cell.height -heights[row][col]);
82-
queue.offer(newCell(row,col,Math.max(heights[row][col],cell.height)));
70+
// from the borders, pick the shortest cell visited and check its neighbors:
71+
// if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped
72+
// add all its neighbors to the queue.
73+
int[][]dirs =newint[][] {{-1,0}, {1,0}, {0, -1}, {0,1}};
74+
intres =0;
75+
while (!queue.isEmpty()) {
76+
Cellcell =queue.poll();
77+
for (int[]dir :dirs) {
78+
introw =cell.row +dir[0];
79+
intcol =cell.col +dir[1];
80+
if (row >=0 &&row <m &&col >=0 &&col <n && !visited[row][col]) {
81+
visited[row][col] =true;
82+
res +=Math.max(0,cell.height -heights[row][col]);
83+
queue.offer(newCell(row,col,Math.max(heights[row][col],cell.height)));
84+
}
8385
}
8486
}
85-
}
8687

87-
returnres;
88+
returnres;
89+
}
8890
}
8991
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp