Leetcode 473.火柴拼正方形
火柴拼正方形
还记得童话《卖火柴的小女孩》吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法。不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到。
输入为小女孩拥有火柴的数目,每根火柴用其长度表示。输出即为是否能用所有的火柴拼成正方形。
示例 1:
输入: [1,1,2,2,2]
输出: true
解释: 能拼成一个边长为2的正方形,每边两根火柴。
示例 2:
输入: [3,3,3,3,4]
输出: false
解释: 不能用所有火柴拼成一个正方形。
注意:
- 给定的火柴长度和在 0 到 10^9之间。
- 火柴数组的长度不超过15。
想象正方形的4条边是4个桶,将每个火柴棍回溯放置在每个桶中,放完N个后,检查4个桶中的长度和是否相同
优化剪枝:
1.N个火柴棍的总和对4取余是不是0,不是的话返回假
2.长度按照从大到小排序,先尝试长的,减少回溯的可能
3.每次放置时,每条边上不可放置超过总和1/4长度的火柴棍
import java.util.Arrays; class Solution {
public boolean makesquare(int[] nums) {
if(nums.length<4) return false;
int sum=0;
for(int i=0;i<nums.length;i++) sum+=nums[i];
if(sum%4!=0) return false;
Arrays.sort(nums);
int[] bucket=new int[4];
return generate(0,nums,sum/4,bucket);
} public boolean generate(int i,int[] nums,int target,int[] bucket){
if(i==nums.length) return bucket[0]==target&&bucket[1]==target&&bucket[2]==target&&bucket[3]==target;
for(int j=0;j<4;j++){
if(bucket[j]+nums[i]>target) continue;
bucket[j]+=nums[i];
if(generate(i+1,nums,target,bucket)) return true;
bucket[j]-=nums[i];
}
return false;
}
}
Leetcode 473.火柴拼正方形的更多相关文章
- Java实现 LeetCode 473 火柴拼正方形
473. 火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到 ...
- leetcode 473. 火柴拼正方形(DFS,回溯)
题目链接 473. 火柴拼正方形 题意 给定一串数,判断这串数字能不能拼接成为正方形 思路 DFS,但是不能每次从从序列开始往下搜索,因为这样无法做到四个边覆盖不同位置的值,比如输入是(5,5,5,5 ...
- Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)
Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...
- 473 Matchsticks to Square 火柴拼正方形
还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到.输入为小女孩拥有火柴的 ...
- [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- C#版 - Leetcode 593. 有效的正方形 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- hdu 1518 拼正方形
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题意:输入几个长度,判断能否拼成正方形. 以下部分参考了网友代码,终于ac啦. #include ...
- [LeetCode] Valid Square 验证正方形
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
随机推荐
- Burpsuite Professional安装及使用教程
转自:https://www.jianshu.com/p/edbd68d7c341 1.先从吾爱破解论坛下载工具:https://down.52pojie.cn/Tools/Network_Analy ...
- 【虚拟机-远程连接】Azure Linux 虚拟机常见导致无法远程的操作
对Azure虚拟机的一些操作可能会导致无法远程连接,本文罗列了以下导致不能远程连接的场景: 场景1 - 在虚拟机配置IP地址或MAC地址 场景2 - 错误地修改服务的配置文件 场景3 - 误设置防火墙 ...
- python之map,zip,reduce,filter的用法
1.reduce(func,iterable,initial): 参数: - func 可执行函数 - iterable 可迭代对象 - initial 可选,初始参数 功能描述:调用func函数后, ...
- SQL SERVER之填充因子
建SQL SERVER索引的时候有一个选项,即Fillfactor(填充因子). 这个可能很少人会去注意它,但它也是比较重要的.大家可能也都知道有这个东西,但是如何去使用它,可能会比较迷糊.另外,即使 ...
- 将服务器上的文件通过HttpWebRequest下载到本地
外网地址需要先映射. string path=""; path=@"http://222.92.71.116/P2Foundation/Images/logo.gif&q ...
- BZOJ 4563: [Haoi2016]放棋子
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 389 Solved: 248[Submit][Status][Discuss] Descriptio ...
- SAP Cloud for Customer Price-计价简介
SAP Cloud for Customer(本文以下简称C4C)作为SAP新一代的CRM云产品,其Price功能实现虽不如以前的SAP ERP那么复杂,但是也能满足企业运作中各种Price需求. C ...
- 第010课_掌握ARM芯片时钟体系
from:第010课_掌握ARM芯片时钟体系 第001节_S3C2440时钟体系结构 S3C2440是System On Chip(SOC),在芯片省不仅仅有CPU,还有一堆外设. 至于有哪些外设,可 ...
- event loop、进程和线程、任务队列
本文原链接:https://cloud.tencent.com/developer/article/1106531 https://cloud.tencent.com/developer/articl ...
- Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fundService': Injection of resource dependencies failed;
在进行SSM的Controller的编写, 从浏览器访问后端Controller的时候遇到了这个问题. 这个问题的描述: 创建Bean的对象失败 错误代码如下: @Service("fund ...