题目要求

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 解题报告的更多相关文章

  1. 【LeetCode】427. Construct Quad Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 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 ...

  3. 【leetcode】427. Construct Quad Tree

    problem 427. Construct Quad Tree 参考 1. Leetcode_427. Construct Quad Tree; 完

  4. [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 ...

  5. 【LeetCode】100. Same Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  6. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  7. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

  8. 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 ...

  9. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. ffmpeg主体架构分析

    [时间:2016-07] [状态:Open] [关键词:ffmpeg,libavcodec,libavformat] FFmpeg接触几年了,用的比较多的是libavcodec和libavformat ...

  2. PentesterLab渗透演练平台

    转载自: https://www.blackh4t.org/archives/1143.html http://www.91ri.org/5958.html     1.  什么是WebApp Pen ...

  3. RHEL/CentOS 一些不错的第三方软件包仓库

    被Debian/Ubuntu惯坏了之后,再来用 RHEL/CentOS 实在是有点不习惯,软件都老且不说,关键是你还别嫌老(5年一个大版本的节奏真受不了啊,RHEL 6上面还是python 2.6), ...

  4. 【iCore4 双核心板_ARM】例程七:WWDG看门狗实验——复位ARM

    实验原理: STM32内部包含窗口看门狗,通过看门狗可以监控程序运行,程序运行错误时,未在 规定时间喂狗(提前或超时),自动复位ARM,本实验通过按键按下,停止喂狗,从而产 生复位. 核心代码: in ...

  5. (笔记)Linux内核学习(一)之内核介绍

    内核与操作系统: 内核是操作系统的核心部分,包含了系统运行的核心过程,决定系统的性能,操作系统启动内核被装入到RAM中: 操作系统与底层硬件设备交互和为运行应用程序提供执行环境. Linux内核与微内 ...

  6. 响应式编程笔记三:一个简单的HTTP服务器

    # 响应式编程笔记三:一个简单的HTTP服务器 本文我们将继续前面的学习,但将更多的注意力放在用例和编写实际能用的代码上面,而非基本的APIs学习. 我们会看到Reactive是一个有用的抽象 - 对 ...

  7. CentOS最小化安装(一)

    一.配置网络 切记记得配置DNS,否则Ping不通 在目录中进行网络配置文件的查找:  /etc/sysconfig/network-scripts/ 1   1 /etc/sysconfig/net ...

  8. duilib进阶教程 -- Label控件的bug (8)

    上个教程说到了TreeView的文字不能垂直居中的问题,而我们用LabelUI其实是可以垂直居中的,为什么不说是TreeView的bug,而说是Label控件的bug呢?因为影响TreeView垂直居 ...

  9. .net 上传文件大小限制

    1.ii. System.web下修改节点HttpRuntime为 <system.web> <compilation debug="true" targetFr ...

  10. NIO相关概念之Selector

    选择器(selector): 选择器管理者一个被注册的通道的集合信息和它们的就绪状态.通道是和选择器一起被注册的,并且使用选择器来更新通道的就绪状态,当这么做的时候,可以选择被激发的线程挂起,直到有就 ...