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 ...
随机推荐
- python爬虫之路——初识爬虫三大库,requests,lxml,beautiful.
三大库:requests,lxml,beautifulSoup. Request库作用:请求网站获取网页数据. get()的基本使用方法 #导入库 import requests #向网站发送请求,获 ...
- ASUS主板 Type C 接口无效问题
修改UEFI设置,把 USB TYPE C POWER SWITCH 改成启用
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B "Or" Game (贪心)
首先应该保证二进制最高位尽量高,而位数最高的数乘x以后位数任然是最高的,所以一定一个数是连续k次乘x. 当出现多个最高位的相同的数就枚举一下,先预处理一下前缀后缀即可. #include<bit ...
- 撤销git pull命令
比如:在master分支上执行了git pull命令,想回到pull之前分支所在的commit位置. 步骤一:用 git reflog master 查看master分支的历史变动记录,其中有一个就是 ...
- js中读取解析json数据
在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键. JSON字符串: 'var str1 = ' ...
- Java中的ArrayList类和LinkedList
集合的体系: ----------| Collection 单列集合的根接口----------------| List 如果实现了List接口的集合类,具备的特点: 有序,可重复.--------- ...
- jqury点击返回顶部代码
效果请看右下角:代码如下: <div class="totop"><img src="https://www.cnblogs.com/images/cn ...
- 巧用 Odoo act_window 的 flags实现一些个性化的视图控制
转自:http://www.khcloud.net:4082/?thread-58.htm 'flags': { 'sidebar': False, //是否显示sidebar区域(主要为action ...
- A. Vitya in the Countryside
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- 配置基于Vim的Python开发环境
配置基于Vim的Python开发环境插件 Vundle YouCompleteMe NERDTree Vim-Jinja2-Syntax set nocompatible " be iMpr ...