题目要求

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 valisLeaf 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. 不8000就业,不还实习费的AICODER全栈实习二期开始报名

    4月17日是个伟大的日子,AICODER全栈实习一期班,正式开始!伙伴们已经撸起袖子加油干了. 二期班开始报名 二期班定于5月17日开班,从二期班开始,实习费用调整如下: 三个月模式实习费,调整为12 ...

  2. 重定向如何携带cookie

    起因 最近在做微信开放平台,需要给第三方入住,而且入住方都有自己的二级域名.做过微信开发的人都知道,坑爹的是微信并不支持这种二级域名的方式,所以用一个域名专门来处理. 问题 然后由于采用了一个专门的域 ...

  3. HyperLogLog

    数据量一大,连统计基数也成了一个麻烦事.在使用kylin的时候,遇到对度量值进行基数统计,使用的是Hyperloglog算法,占用内存小,误差小,实乃不错的方法,但查阅网上的资料与内容,感觉未能理解的 ...

  4. Java多线程系列——线程池原理之 ThreadPoolExecutor

    ThreadPoolExecutor 简介 ThreadPoolExecutor 是线程池类. 通俗的讲,它是一个存放一定数量线程的线程集合.线程池允许多个线程同时运行,同时运行的线程数量就是这个线程 ...

  5. Kiss MySQL goodbye for development and say hello to HSQLDB

    The days of using MySQL, DB2, PostgreSQL etc for development is  over.. I don’t know why any program ...

  6. linux选择sdb sdb4 fat32 还是sda分区

    fat32是怎么混到它们中的sda,sdb,sdc是你的第一块,第二块,第三块硬盘sda1,sda2,sda5是你第一块硬盘中的第一块分区,2块,5块分区fat32,ext2,ext3,ext4是你的 ...

  7. Java Web项目中连接Access数据库的配置方法

    本文是对前几天的"JDBC连接Access数据库的几种方式"这篇的升级.因为在做一些小项目的时候遇到的问题,因此才决定写这篇博客的.昨天已经将博客公布了.可是后来经过一些验证有点问 ...

  8. [PGM] What is Probabalistic Graphical Models

    学术潜规则: 概率图模型提出的意义在于将过去看似零散的topic/model以一种统一的方式串联了起来,它便于从整体上看待这些问题,而非具体解决了某个细节. 举个例子:梯度下降,并非解决神经网络收敛问 ...

  9. 15适配器模式Adapter

    一.什么是适配器模式 Adapter模式也叫适配器模式,是构造型模式之一 ,通过Adapter模式可以改变已有类(或外部类)的接 口形式. 二.适配器模式应用场景 在大规模的系统开发过程中,我们常常碰 ...

  10. Android打印当前所有线程及对应栈信息

    Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces(); Log.e("albertThr ...