当前位置:网站首页>Summary of force deduction solution 427- establishment of quadtree
Summary of force deduction solution 427- establishment of quadtree
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
Original link : Power button
describe :
To give you one n * n matrix grid , The matrix consists of several 0 and 1 form . Please use a quadtree to represent the matrix grid .
You need to return something that represents a matrix quadtree Root node of .
Be careful , When isLeaf by False when , You can take True perhaps False Assign to node , Both values will be judged by the problem determination mechanism Accept .
Quadtree data structure , Each internal node has only four child nodes . Besides , Each node has two properties :
val: Store the value of the area represented by the leaf node .1 Corresponding True,0 Corresponding False;
isLeaf: When this node is a leaf node, it is True, If it has 4 The child nodes are False .
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
}
We can build a quadtree for a two-dimensional region as follows :
If the values of the current grid are the same ( namely , All for 0 Or all 1), take isLeaf Set to True , take val Set to the corresponding value of the grid , And set all four child nodes to Null Then stop .
If the value of the current grid is different , take isLeaf Set to False, take val Set to any value , Then as shown in the figure below , Divides the current mesh into four sub meshes .
Recurse each child node using the appropriate sub mesh .
If you want to know more about quadtrees , You can refer to wiki .
Quadtree format :
The output is the serialized form of quadtree after sequence traversal , among null Indicates the path terminator , There is no node below it .
It is very similar to the serialization of binary trees . The only difference is that nodes are represented in a list [isLeaf, val] .
If isLeaf perhaps val The value of is True , It is in the list [isLeaf, val] The value of 1 ; If isLeaf perhaps val The value of is False , Then the value is 0 .
Example 1:
Input :grid = [[0,1],[1,0]]
Output :[[0,1],[1,0],[1,1],[1,1],[1,0]]
explain : The explanation of this example is as follows :
Please note that , In the diagram of the quadtree below ,0 Express false,1 Express True .
Example 2:
Input :grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
Output :[[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
explain : All values in the grid are different . We divide the grid into four sub grids .
topLeft,bottomLeft and bottomRight All have the same value .
topRight Have different values , So we subdivide it into 4 Sub grid , In this way, each sub grid has the same value .
The explanation is shown in the figure below :
Example 3:
Input :grid = [[1,1],[1,1]]
Output :[[1,1]]
Example 4:
Input :grid = [[0]]
Output :[[1,0]]
Example 5:
Input :grid = [[1,1,0,0],[1,1,0,0],[0,0,1,1],[0,0,1,1]]
Output :[[0,1],[1,1],[1,0],[1,0],[1,1]]
Tips :
n == grid.length == grid[i].length
n == 2^x among 0 <= x <= 6
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/construct-quad-tree
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Construct a recursive method , The input parameters are array and range . * In this recursive method , If the values are all the same , Returns a Node Node isLeaf=true. * Otherwise, call recursively four times , Find the upper left , The upper right , The lower left , Four ranges on the lower right Node value .
Code :
public class Solution427 {
public Node construct(int[][] grid) {
Node construct = construct(grid, 0, grid[0].length, 0, grid.length);
return construct;
}
public Node construct(int[][] grid, int startX, int endX, int startY, int endY) {
// Whether the values are the same
boolean isEqual = true;
int value = -1;
flag:
for (int y = startY; y < endY; y++) {
for (int x = startX; x < endX; x++) {
if (value == -1) {
value = grid[y][x];
continue;
}
if (value == grid[y][x]) {
continue;
}
isEqual = false;
break flag;
}
}
Node node = new Node();
if (isEqual) {
node.val = value == 1;
node.isLeaf = true;
return node;
}
node.val = false;
node.isLeaf = false;
int middleX = startX + (endX - startX) / 2;
int mideleY = startY + (endY - startY) / 2;
node.topLeft = construct(grid, startX, middleX, startY, mideleY);
node.topRight = construct(grid, middleX, endX, startY, mideleY);
node.bottomLeft = construct(grid, startX, middleX, mideleY, endY);
node.bottomRight = construct(grid, middleX, endX, mideleY, endY);
return node;
}
static class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node() {
this.val = false;
this.isLeaf = false;
this.topLeft = null;
this.topRight = null;
this.bottomLeft = null;
this.bottomRight = null;
}
public Node(boolean val, boolean isLeaf) {
this.val = val;
this.isLeaf = isLeaf;
this.topLeft = null;
this.topRight = null;
this.bottomLeft = null;
this.bottomRight = null;
}
public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
this.val = val;
this.isLeaf = isLeaf;
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}
}
}边栏推荐
- 超图倾斜数据合并根节点后转3dtiles
- Basedexclassloader
- How WPS inserts a directory and the operating steps for quickly inserting a directory
- Graphic data analysis | data cleaning and pretreatment
- Linux (centos7) installer mysql - 5.7
- Installing MySQL version 5.5 database for Linux (centos6)
- Four schemes for redis to implement message queue
- 力扣解法汇总-剑指 Offer II 114. 外星文字典
- How can low code platforms improve cost effectiveness?
- Google Ads 的优势
猜你喜欢

el-upload上传文件

ozzanimation-基于sse的动作系统

ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
![[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology](/img/16/2f9a235995cdd54ac9b85a68a7afcb.jpg)
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology

Graphical data analysis | business analysis and data mining

Explore performance optimization! Performance improvement from 2 months to 4 hours!

How WPS inserts a directory and the operating steps for quickly inserting a directory

Why do we use Google search ads?

How to stop anti-virus software from blocking a web page? Take gdata as an example

Installing MySQL version 5.5 database for Linux (centos6)
随机推荐
php安全开发 12博客系统的 系统模块信息的修改
2022最全面的Redis事务控制(带图讲解)
力扣解法汇总875-爱吃香蕉的珂珂
Swiftyjson analyse les fichiers json locaux
打包一个包含手表端应用的手机端APK应用—Ticwear
力扣解法汇总905-按奇偶排序数组
力扣解法汇总473-火柴拼正方形
Redis cluster + sentinel mode + replicas
The establishment and introduction of the announcement module of PHP development blog system
php开发 博客系统的公告模块的建立和引入
PHP builds a high-performance API architecture based on sw-x framework (III)
力扣解法汇总面试题 17.11-单词距离
Pagination writing of PHP security open 10 article list module
2022福建省安全员C证(专职安全员)考试模拟100题及答案
自适应搜索广告有哪些优势?
力扣解法汇总942-增减字符串匹配
Make ads more relevant by searching for additional information about ads
[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin
2022西式面点师(技师)复训题库及在线模拟考试
力扣解法汇总868-二进制间距