题目要求

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. 【九天教您南方cass 9.1】 09 提取坐标的几种方法

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取测量空间中. [点击索取cass教程]5元立得 (给客服说暗号:“ ...

  2. 【转】燃烧吧,TestMice!

    ...当我们几个人碰面的时候,就感觉应该做点测试业内的实事. 记得当时的17站出了一些QTP辅件,给了我一些灵感.2008年做了一整年的QTP企业级实施,从方案到最后的收尾支持,得到最大的教训就是,当 ...

  3. Java如何验证电子邮件地址格式?

    在Java编程中,如何验证电子邮件地址格式? 以下示例演示如何使用String类的matches()方法来验证电子邮件地址. package com.yiibai; public class Vali ...

  4. Spark学习笔记——构建基于Spark的推荐引擎

    推荐模型 推荐模型的种类分为: 1.基于内容的过滤:基于内容的过滤利用物品的内容或是属性信息以及某些相似度定义,来求出与该物品类似的物品. 2.协同过滤:协同过滤是一种借助众包智慧的途径.它利用大量已 ...

  5. tensorflow随机梯度下降算法使用滑动平均模型

    在采用随机梯度下降算法训练神经网络时,使用滑动平均模型可以提高最终模型在测试集数据上的表现.在Tensflow中提供了tf.train.ExponentialMovingAverage来实现滑动平均模 ...

  6. 【NLP】分词 新词

    基于大规模语料的新词发现算法 https://blog.csdn.net/xgjianstart/article/details/52193258 互联网时代的社会语言学:基于SNS的文本数据挖掘 h ...

  7. python网络编程之UDP方式传输数据

    UDP --- 用户数据报协议(User Datagram Protocol),是一个无连接的简单的面向数据报的运输层协议. UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能 ...

  8. 配置openssh实现sftp远程文件上传

    客服端:winscp等ftp/sftp客户端 服务器:阿里云默认使用的openssh 需求:可以sftp远程传输文件到服务器固定文件夹下,不可远程ssh登录 步骤: 1. 建立系统用户ftpuser及 ...

  9. 删除新版UniAccess Agent 办公室监控软件的方法

    UniAccess Agent 是在由LeagSoft开发的监控软件,老版本的一般安装在C:\Program Files\LeagSoft\UniAccess Agent这个目录下,一般找到这个目录点 ...

  10. Xshell登录Ubuntu12.04

    Ubuntu安装ssh服务: sudo apt-get install openssh-server 打开Xshell,选择“新建”,“连接”设置里选择SSH,主机填入需要连接的主机的IP地址.在“用 ...