题目如下:

解题思路:居然把卖火柴的小女孩都搬出来了。题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等。首先我们可以很容易的求出每个子数组的和应该是avg = sum(nums)/4,接下来我的思路是求出nums中所有和等于avg的子数组,子数组中保存元素下标,然后找出四个不存在相同下标的子数组求并集,如果并集长度刚好等于len(nums),那么就是符合条件的,返回true。如果在子数组列表中找到四个符合条件的子数组,我用的是DFS,为什么不用BFS?因为题目要求是找到一组符合条件的子数组即可,DFS显然要比BFS快。

代码如下:

class Solution(object):
def makesquare(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
nums.sort()
border = 4
if sum(nums)%border != 0 :
return False
avg = sum(nums)/border
queue = [[x] for x in xrange(len(nums))]
res = []
visit = [0 for x in xrange(len(nums))]
while len(queue) > 0:
nl = queue.pop(0)
amount = 0
for i in nl:
amount += nums[i]
if amount == avg:
res.append(nl)
for i in nl:
visit[i] = 1
continue
tl = []
for i in xrange(nl[-1]+1,len(nums)):
if amount + nums[i] <= avg:
tl = nl[:]
tl.append(i)
queue.append(tl)
if len(res) < border:
return False
if sum(visit) != len(visit):
return False
queue = []
for i in res:
queue.append((set(i),1))
#print queue
while len(queue) > 0:
ns,count = queue.pop(0)
if count == border and len(ns) == len(nums):
#print ns
return True
for i in res:
#print ns | set(i)
if len(ns | set(i)) == len(ns) + len(i):
queue.insert(0,(ns|set(i),count+1)) return False

【leetcode】473. Matchsticks to Square的更多相关文章

  1. 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)

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

  2. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  3. 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...

  4. 【leetcode】367. Valid Perfect Square

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  5. 【LeetCode】422. Valid Word Square 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...

  6. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  7. 【leetcode】633. Sum of Square Numbers(two-sum 变形)

    Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...

  8. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  9. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. HttpClient设置忽略SSL,实现HTTPS访问, 解决Certificates does not conform to algorithm constraints

    话不多说,直接上代码. 测试API:   https://api.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f7 ...

  2. 正则表达式——Unicode

    第 7 章 Unicode 7.1 关于编码   通常,英文编码较为统一,都采用ASCII编码或可以兼容ASCII编码(即编码表的前127位与ASCII编码一直,常见的各种编码,包括Unicode编码 ...

  3. mysql源码包安装

    一.准备编译环境 # yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake # wget ht ...

  4. Hello World!----html

    ​ 最近要做一个小网站,今晚想起来还是先看看前端终于抑制住惰性,开始看了. ​ 看了一下html,写了个hello world.老实讲,我竟然还有些小激动 ​ <html> <hea ...

  5. ionic3构建过程中遇到的找不到AndroidManifest.xml的问题

    问题如下: Failed to install 'ionic-plugin-keyboard': Error: ENOENT: no such file or directory, open '/Us ...

  6. Mybatis-学习笔记(7)缓存机制

    1.一级缓存 SqlSession级别的缓存,使用HashMap存储缓存数据,不同的SqlSession之间的缓存数据区域(HashMap)互不影响. 一级缓存的作用域是SqlSession范围(强调 ...

  7. BAT推荐免费下载JAVA转型大数据开发全链路教程(视频+源码)价值19880元

    如今随着环境的改变,物联网.AI.大数据.人工智能等,是未来的大趋势,而大数据是这些基石,万物互联,机器学习都是大数据应用场景! 为什么要学习大数据?我们JAVA到底要不要转型大数据? 好比问一个程序 ...

  8. 概率与期望dp相关

    概率与期望dp 概率 某个事件A发生的可能性的大小,称之为事件A的概率,记作P(A). 假设某事的所有可能结果有n种,每种结果都是等概率,事件A涵盖其中的m种,那么P(A)=m/n. 例如投掷一枚骰子 ...

  9. qt 部分控件 setStyleSheet 使用总结

    刚用Qt不久,但是已经感受到Qt ui设计的便捷. 总结一下最近使用的控件,把它们setStyleSheet的使用方法记录下来. 主要使用到的工具有:QToolBar,QToolBox,QPushBu ...

  10. 小白学Python——Matplotlib 学习(3) 函数图形

    import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1,1,50) y = 2*x + 1 plt.figure() ...