题目如下:

解题思路:居然把卖火柴的小女孩都搬出来了。题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等。首先我们可以很容易的求出每个子数组的和应该是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. CentOS7 修改网卡名称为eth0 & 在VMWare中添加多网卡配置

    目录 目录 前言 在CentOS 7 中为什么这样命名网卡 在RHEL7中使用RHEL6的网卡命名规则 在VMWare中为CentOS7添加网卡设备 前言 无论是RHEL 7.还是CentOS 7都使 ...

  2. 编译mysql时CMake Error at cmake/readline.cmake:85 (MESSAGE)

    CMake Error at cmake/readline.cmake:85 (MESSAGE):  Curses library not found.  Please install appropr ...

  3. 日常linux命令

    绝对路径用什么符号表示?当前目录.上层目录用什么表示?主目录用什么表示? 切换目录用什么命令?   绝对路径: 如/etc/init.d                当前目录和上层目录: ./  . ...

  4. 移动端自动化==>Appium定位方式总结

    1.ID Android Android的resource-id对应ID定位方式,可以通过index来获取需要的元素(从0开始查找dom树中的同名resource-id属性).使用appium-des ...

  5. 11 ORA-8102:Index Corruption解析

    11 ORA-8102:Index Corruption解析 [oracle@DSI ~]$ oerr ora 810208102, 00000, "index key not found, ...

  6. vue组件之间通信总结---点赞

    总结:父组件-->子组件 ①通过属性 步骤1: <son myName="michael" myPhone='123'></son> <son ...

  7. 基于 @Scheduled 注解的 ----定时任务

    最常用的方法@Scheduled 注解表示起开定时任务 依赖 <dependencies> <dependency> <groupId>org.springfram ...

  8. vue-devtools安装以后,勾选了“允许访问文件网址”之后还是无法使用

    勾选了“允许访问文件网址”,还是无法使用: Vue.js is detected on this page. Devtools inspection is not available because ...

  9. 通过QT查找Word中的关键字,并做高亮或删除操作

    最近由于项目需要,要用QT操作Word文档.具体的工作需求:在指定的Word文档(*.doc文件/*.docx文件)中查找关键字,找到后做高亮操作或者直接删除操作,然后另存为到别的目录(表示这个文件被 ...

  10. 【JZOJ 3918】蛋糕

    题面: 正文: 根据数据\(4\leq R,C\leq 75\)我们大概可以先枚举切横的刀再二分答案. 更具体的: 假设我们已经枚举到这样横切: 再假设我们已经二分到最小的巧克力是\(7\) 康康第一 ...