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

Commitba7fd8f

Browse files
Create: 0427-construct-quad-tree.java
Signed-off-by: Aliaksei <a1exandddrovich@gmail.com>
1 parent8a2fea5 commitba7fd8f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎java/0427-construct-quad-tree.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
classSolution {
2+
3+
privateint[][]grid;
4+
5+
publicNodeconstruct(int[][]grid) {
6+
this.grid =grid;
7+
returndfs(0,0,grid.length);
8+
}
9+
10+
privateNodedfs(introw,intcolumn,intn) {
11+
Nodenode =newNode();
12+
13+
if (areAllEqual(row,column,n)) {
14+
node.isLeaf =true;
15+
node.val =grid[row][column] ==1;
16+
}else {
17+
n /=2;
18+
node.isLeaf =false;
19+
node.val =false;
20+
21+
node.topLeft =dfs(row,column,n);
22+
node.bottomLeft =dfs(row +n,column,n);
23+
node.topRight =dfs(row,column +n,n);
24+
node.bottomRight =dfs(row +n,column +n,n);
25+
}
26+
27+
returnnode;
28+
}
29+
30+
privatebooleanareAllEqual(introw,intcolumn,intn) {
31+
for (inti =0;i <n;i++) {
32+
for (intj =0;j <n;j++) {
33+
if (grid[row][column] !=grid[row +i][column +j]) {
34+
returnfalse;
35+
}
36+
}
37+
}
38+
39+
returntrue;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp