-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPondSizes.java
More file actions
41 lines (39 loc) · 1.12 KB
/
PondSizes.java
File metadata and controls
41 lines (39 loc) · 1.12 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
package leetcode.array;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName PondSizes
* @Description
* @Author changxuan
* @Date 2020/12/2 下午9:31
**/
public class PondSizes {
public int[] pondSizes(int[][] land) {
List<Integer> list = new ArrayList<>();
int temp = 0;
for(int i = 0;i<land.length;i++){
for(int j=0;j<land[0].length;j++){
if(land[i][j]==0){
temp = dfs(land,i,j);
list.add(temp);
}
}
}
list.sort((o1,o2)-> o1-o2);
return list.stream().mapToInt(Integer::intValue).toArray();
}
private int dfs(int [][]land,int i,int j){
if(i<0||j<0||i>=land.length||j>=land[0].length||land[i][j]!=0) return 0;
land[i][j] = 3;
int area = 1;
area += dfs(land,i-1,j-1);
area += dfs(land,i-1,j+1);
area += dfs(land,i,j+1);
area += dfs(land,i,j-1);
area += dfs(land,i-1,j);
area += dfs(land,i+1,j);
area += dfs(land,i+1,j-1);
area += dfs(land,i+1,j+1);
return area;
}
}