LeetCode 427 Construct Quad Tree 解题报告
题目要求
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same.
Each node has another two boolean attributes : isLeaf and val. isLeaf is true if and only if the node is a leaf node. The val attribute for a leaf node contains the value of the region it represents.
题目分析及思路
需要使用quad trees来存储 N x N的布尔值网格,每个网格只能是true或false。根结点表示整个网格。对于每一个结点,要求将自身平均分成四分,直到每一份的值都相等为止。每一个结点还有另外两个布尔属性isLeaf和val。isLeaf是True当且仅当这个结点是叶结点。val则是表示当前叶结点的值,若不是叶结点,则用“*”表示。可以先写一个函数来判断这个区域的值是否相同,之后用递归的方法求解,条件是该结点是叶结点。
python代码
"""
# Definition for a QuadTree node.
class Node:
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
self.val = val
self.isLeaf = isLeaf
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
"""
class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
def isthesame(grid):
e = set()
for r in range(len(grid)):
for c in range(len(grid)):
e.add(grid[r][c])
if len(e) == 1 and 1 in e:
return True
elif len(e) == 1 and 0 in e:
return False
else:
return '*'
if isthesame(grid) == True:
return Node(True, True, None, None, None, None)
elif isthesame(grid) == False:
return Node(False, True, None, None, None, None)
else:
l = len(grid)
mid = l // 2
topleft = [[grid[i][j] for j in range(mid)] for i in range(mid)]
topright = [[grid[i][j] for j in range(mid, l)] for i in range(mid)]
bottomleft = [[grid[i][j] for j in range(mid)] for i in range(mid, l)]
bottomright = [[grid[i][j] for j in range(mid, l)] for i in range(mid, l)]
return Node('*', False, self.construct(topleft), self.construct(topright), self.construct(bottomleft), self.construct(bottomright))
LeetCode 427 Construct Quad Tree 解题报告的更多相关文章
- 【LeetCode】427. Construct Quad Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- leetcode 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- 【leetcode】427. Construct Quad Tree
problem 427. Construct Quad Tree 参考 1. Leetcode_427. Construct Quad Tree; 完
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- 【LeetCode】100. Same Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- LeetCode 965 Univalued Binary Tree 解题报告
题目要求 A binary tree is univalued if every node in the tree has the same value. Return true if and onl ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- TCP拥塞控制-慢启动、拥塞避免、快重传、快启动
一般原理:发生拥塞控制的原因:资源(带宽.交换节点的缓存.处理机)的需求>可用资源. 作用:拥塞控制就是为了防止过多的数据注入到网络中,这样可以使网络中的路由器或者链路不至于过载.拥塞控制要做的 ...
- 9patch图的尺寸尽量为偶数
美工做了一张.9的背景图,宽度110*80 像素,描点如下: 放到720p的智能电视上观看,总感觉怪怪的.仔细观看可以发现,前景图总是不能完全的覆盖掉背景图.总有那么一个像素的点多余出来,如图所示: ...
- Go Revel - Testing(测试模块)
revel提供了一个测试框架来方便的为自己的程序编写功能测试用例. 默认创建的应用骨架附带一个简单的测试用例,这里将它作为起点 ##概览 测试保存在`tests`目录: corp/myapp app/ ...
- Nginx 1.9+PHP5.6 环境搭建
PHP5. 下载安装包 #wget http://mirrors.sohu.com/php/php-5.6.2.tar.gz #tar -zxf php- 安装php依赖的包 #yum inst ...
- Java知多少(86)文本框和文本区的输入输出
在GUI中,常用文本框和文本区实现数据的输入和输出.如果采用文本区输入,通常另设一个数据输入完成按钮.当数据输入结束时,点击这个按钮.事件处理程序利用getText()方法从文本区中读取字符串信息.对 ...
- Python爬虫学习——使用selenium和phantomjs爬取js动态加载的网页
1.安装selenium pip install selenium Collecting selenium Downloading selenium-3.4.1-py2.py3-none-any.wh ...
- [JS] ECMAScript 6 - Destructuring
C#里没有这种变态的方法. 虽然变态,但看起来不错的样子. 变量的解构赋值 完全解构:本质上,这种写法属于“模式匹配”,: 不完全解构:同时支持“不完全解构” let [x, y] = [1, 2, ...
- post请求参数设置
控制器参数有[FromBody]修饰参数这么传: 控制器没有[FromBody]修饰参数这么传:
- UML中类结构图示例
- android 网络图片双缓存
本文转自:http://blog.csdn.net/a79412906/article/details/10180583 Android每次加载图片很浪费时间.所以设计了一个图片缓存技术来解决每次an ...