【leetcode】473. Matchsticks to Square
题目如下:

解题思路:居然把卖火柴的小女孩都搬出来了。题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等。首先我们可以很容易的求出每个子数组的和应该是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的更多相关文章
- 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- 【leetcode】367. Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- 【LeetCode】422. Valid Word Square 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【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. ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- java 发送 http post 和 get 请求(利用unirest)
调用服务器端的接口在前端调用,但是我们也会经常遇到在服务器后端调用接口的情况,网上的例子大部分都是用 jdk 原生的 URL realUrl = new URL(url); URLConnection ...
- Intellij Idea使用教程汇总篇
Java编程强大的工具IDEA使用教程及一些快捷键收藏如下: https://blog.csdn.net/fanrenxiang/article/details/80503490
- pandas 数据排序.sort_index()和.sort_values()
原文链接:https://www.jianshu.com/p/f0ed06cd5003 import pandas as pd df = pd.DataFrame(……) 说明:以下“df”为Data ...
- Insertion Sort List(单链表插入排序)
来源:https://leetcode.com/problems/insertion-sort-list Sort a linked list using insertion sort. 方法: 1. ...
- Java IO NIO详细讲解
1.IO Java IO概述 2.NIO Java NIO浅析
- Java json框架简介
Gson,FastJson,Jackson,Json-lib性能比较 https://www.xncoding.com/2018/01/09/java/jsons.html 结论:FastJson虽然 ...
- 【SSL2325】最小转弯问题
题面: \[\Large\text{最小转弯问题}\] \[Time~Limit:1000MS~~Memory~Limit:65536K\] Description 给出一张地图,这张地图被分为 n× ...
- jquery中的插件EChars的使用
首先,进入EChars的官网下载页面:http://echarts.baidu.com/download.html 下载自己需要的版本. 引入jquery包和echars,进入官网的实例:htt ...
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
出现错误 Client does not support authentication protocol requested by server; consider upgrading MySQL c ...
- webpack自定义loader和自定义插件
1.封装自定义的功能loader (格式很简单,重点在于loader-utils,loaderUitls.getOptions可获取webpack配置rules中的options以供使用 ) 这只是一 ...