作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/construct-quad-tree/description/

题目描述

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.

Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:

Given the 8 x 8 grid below, we want to construct the corresponding quad tree:

It can be divided according to the definition above:

The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val) pair.

For the non-leaf nodes, val can be arbitrary, so it is represented as *.

Note:

  1. N is less than 1000 and guaranteened to be a power of 2.
  2. If you want to know more about the quad tree, you can refer to its wiki.

题目大意

题目很长,但是不要害怕。题目所说的四分树,其实就是平面上的一种集合结构。把一个边长为2的幂的正方形均分成4块,然后再均分到不能均分为止即为叶子节点。类似于树结构,这是一个平面结构。

解题方法

首先,这种结构和树结构非常类似,肯定使用递归求解。重要的是如何判断此树结构如何判断叶子节点、val。

所以定义了一个新的函数,如果一个正方形中所有的数字都是0,则val是False,否则val是True。

判断leaf的方法是看看格子里的所有的值是不是相同的,如果全是0或者1那么就是leaf,否则就不是。

本来应该是两个函数,但是我用一个函数有3个返回值就实现了。

其他的难点就在把正方形进行切分成四块了,这个不是难点。

代码如下:

"""
# 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):
"""
:type grid: List[List[int]]
:rtype: Node
"""
isLeaf = self.isQuadTree(grid)
_len = len(grid)
if isLeaf == None:
mid = _len // 2
topLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid)]
topRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid)]
bottomLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid, _len)]
bottomRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid, _len)]
node = Node(True, False, self.construct(topLeftGrid), self.construct(topRightGrid),
self.construct(bottomLeftGrid), self.construct(bottomRightGrid))
elif isLeaf == False:
node = Node(False, True, None, None, None, None)
else:
node = Node(True, True, None, None, None, None)
return node def isQuadTree(self, grid):
_len = len(grid)
_sum = 0
for i in range(_len):
_sum += sum(grid[i])
if _sum == _len ** 2:
return True
elif _sum == 0:
return False
else:
return None

二刷,换了一种写法,也是递归。

"""
# Definition for a QuadTree node.
class Node(object):
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(object):
def construct(self, grid):
"""
:type grid: List[List[int]]
:rtype: Node
"""
N = len(grid)
if N == 1:
return Node(grid[0][0] == 1, True, None, None, None, None)
topLeftSum = sum([grid[i][j] for i in range(N/2) for j in range(N/2)])
topRightSum = sum([grid[i][j] for i in range(N/2) for j in range(N/2, N)])
bottomLeftSum = sum([grid[i][j] for i in range(N/2, N) for j in range(N/2)])
bottomRightSum = sum(grid[i][j] for i in range(N/2, N) for j in range(N/2, N))
node = Node(False, False, None, None, None, None)
if topLeftSum == topRightSum == bottomLeftSum == bottomRightSum:
if topLeftSum == 0:
node.isLeaf = True
node.val = False
elif topLeftSum == (N / 2) ** 2:
node.isLeaf = True
node.val = True
if node.isLeaf:
return node
node.val = True
node.topLeft = self.construct([[grid[i][j] for j in range(N/2)] for i in range(N/2)])
node.topRight = self.construct([[grid[i][j] for j in range(N/2, N)] for i in range(N/2)])
node.bottomLeft = self.construct([[grid[i][j] for j in range(N/2)] for i in range(N/2, N)])
node.bottomRight = self.construct([[grid[i][j] for j in range(N/2, N)] for i in range(N/2, N)])
return node

日期

2018 年 8 月 19 日 —— 天阴阴,地潮潮,这种天气真舒服
2018 年 11 月 13 日 —— 时间有点快

【LeetCode】427. Construct Quad Tree 解题报告(Python)的更多相关文章

  1. 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 tru ...

  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】654. Maximum Binary Tree 解题报告 (Python&C++)

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

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

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

  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】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

随机推荐

  1. urllib的基本使用介绍

    1. urllib中urlopen的基本使用介绍 1 ### urllib中urlopen的基本使用介绍 2 3 ## urlopen的基本用法(GET请求) 4 import urllib.requ ...

  2. CAN总线常见的两种编码格式(Intel/Motorola)

    在汽车电子行业的开发或者测试中,我们经常会看到CAN总线信号的常见的两种编码格式:Intel格式与Motorola格式. 讲解这两种格式之前,我们先来了解一些大端模式和小端模式,会对后面理解这两种编码 ...

  3. 日常Java 2021/10/21

    Java Iterator(迭代器) 如果需要使用iterator类需要从java.util包中引入它 Java Iterator不是一个集合,它是一种访问集合的方法,用于迭代ArrayList和Ha ...

  4. Oracle中的索引

    1.Oracle 索引简介      在Oracle数据库中,存储的每一行数据都有一个rowID来标识.当Oracle中存储着大量的数据时,意味着有大量的rowID,此时想要快速定位指定的rowID, ...

  5. t01_docker安装TiDB

    Docker环境安装TiDB,在官方说明的基础上补充了几个细节,安装记录如下 个人环境-vbox上安装centos7.4系统 CPU:12核24线程,分配给虚拟机12线程 MEM: 48G,分配给虚拟 ...

  6. zookeeper 异常 :stat is not executed because it is not in the whitelist. Connection closed b

    1 .问题 1.启动 zookeeper 后 用指令:  telnet 127.0.0.1 2181 连接 提示输入指令 :stat 后报错,然后关闭连接 2.问题解决: 修改启动指令 zkServe ...

  7. CDN服务的含义

    CDN的全称是Content Delivery Network,即内容分发网络.CDN的基本原理是广泛采用各种缓存服务器,将这些缓存服务器分布到用户访问相对集中的地区或网络中,在用户访问网站时,利用全 ...

  8. I/O流之文件流

    1.文件操作类 File 1.public File(String pathname)//给定一个要操作文件的完整路径 2.public File(File parent,String child)/ ...

  9. 从orderby引发的SQL注入问题的思考

    背景: 某一天准备上线,合完master之后准备发布了,忽然公司的代码安全监测提示了可能在代码中存在sql注入的风险,遂即检查,发现sql注入问题 既然碰到了这个问题,那就了简单了解下sql注入 基础 ...

  10. PHP之CURL实现含有验证码的模拟登录

    博主最近在为学校社团写一个模拟登录教务系统来进行成绩查询的功能,语言当然是使用PHP啦,原理是通过php数据传输神器---curl扩展,向学校教务系统发送请求,通过模拟登录,获取指定url下的内容. ...