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) ...
随机推荐
- Goldengate:ERROR 180 encountered commit SCN that is not greater than the highest SCN already processed
How to recover from Extract ERROR 180 encountered commit SCN that is not greater than the highest SC ...
- Nginx配置WebService、MySQL、SQL Server、ORACLE等代理
首先介绍一下Nginx的基本使用: 注意不要直接双击nginx.exe,这样会导致修改配置后重启.停止nginx无效,需要手动关闭任务管理器内的所有nginx进程 在nginx.exe目录,打开命令行 ...
- 【Java】Java NIO
NIO 为什么要使用 NIO? NIO 的创建目的是为了让 Java 程序员可以实现高速 I/O 而无需编写自定义的本机代码.NIO 将最耗时的 I/O 操作(即填充和提取缓冲区)转移回操作系统,因而 ...
- Java知多少(33)多态对象的类型转换
这里所说的对象类型转换,是指存在继承关系的对象,不是任意类型的对象.当对不存在继承关系的对象进行强制类型转换时,java 运行时将抛出 java.lang.ClassCastException 异常. ...
- 随笔:JS对象无new构造原理
var myFun = function(words) { if (!(this instanceof myFun)) { return new myFun(words); } this.name = ...
- MTK 屏幕旋转90度
http://blog.csdn.net/ouo555/article/details/44806837 1.屏幕显示顺时针旋转90度 lk 横屏logo,顺时针旋转90度显示修改bootable/b ...
- [Stats385] Lecture 04: Convnets from Probabilistic Perspective
本篇围绕“深度渲染混合模型”展开. Lecture slices Lecture video Reading list A Probabilistic Framework for Deep Learn ...
- [Model] VGG16
Jeff: fast.ai lesson 1&2感觉没讲什么干货. ~/keras/keras.json配置后台theano or tensorflow. ~/./theanorc处理器配置 ...
- Web文件上传方法总结大全
1. 表单上传 这是传统的form表单上传,使用form表单的input[type=”file”]控件,可以打开系统的文件选择对话框,从而达到选择文件并上传的目的,它的好处是多浏览器兼容,它是web开 ...
- DevExpress MemoEdit定位到末尾
1: /// <summary> 2: /// 追加文本到MemoEdit中 3: /// </summary> 4: /// <param name="mem ...