【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/ 题目地 ...
随机推荐
- 【命令汇总】XSS payload 速查表
日期:2019-05-15 14:06:21 作者:Bay0net 介绍:收集并且可用的一些 XSS payload,网上的速查表很多,但是测试了下很多 payload 的不可用,这里都是自己能用的 ...
- Servlet生命周期 Servlet获取配置信息 ServletContext
一.Servlet生命周期 实例化 ——> 初始化 ——> 服务 ——> 销毁 出生:(实例化 然后 初始化)tomcat第一次访问,Servlet就出生(默认情况下) 活着:( ...
- 梳理检测论文-Refinement Neural Network
Single-Shot Refinement Neural Network for Object Detection 目录 1. motivation 2. RefineDet 解析(Network ...
- CentOS 7.6 RPM方式安装Oracle19c的过程
1. 下载需要的安装包: 1.1 preinstall http://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/orac ...
- 安装开发环境vs2017+sql2016+tfs2017
安装开发环境vs2017+sql2016+tfs2017 编写人:左丘文 2019-7-16 近一年了,一直没空着手写点什么,跟大家交待下吧,去年一次机会,其实也不完全是去年,因此离开了我工作15年的 ...
- 背包dp相关
0/1背包 给出n个物品,每个物品有Vi的价值和Wi的费用,我们总共有m块钱,求最多能得到多少价值的物品. N<=10^3,m<=10^3 记录方案数?记录输出方案? 输出方案: 对每个d ...
- 列表、元组和range
小知识点 s = " 5 " print(int(s)) print(s.replace(" ","")) 结果: 5 5 id()#获取对 ...
- 小白学Python——Matplotlib 学习(3) 函数图形
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1,1,50) y = 2*x + 1 plt.figure() ...
- Python和Js打印心形
看到一行Python写的代码,会用LovePython输出心形: print('\n'.join([''.join([('LovePython'[(x-y)%10]if((x*0.05)**2+(y* ...
- SCUT - 216 - 宝华科技树
https://scut.online/p/216 演员 把这个当成dp算了半天,各种姿势,好吧,就当练习一下树dp. 假如是每个节点的层数之和,按照dp[i][j]为从i点出发获得j科技的最小费用d ...