473 Matchsticks to Square 火柴拼正方形
还记得童话《卖火柴的小女孩》吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法。不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到。
输入为小女孩拥有火柴的数目,每根火柴用其长度表示。输出即为是否能用所有的火柴拼成正方形。
示例 1:
输入: [1,1,2,2,2]
输出: true
解释: 能拼成一个边长为2的正方形,每边两根火柴。
示例 2:
输入: [3,3,3,3,4]
输出: false
解释: 不能用所有火柴拼成一个正方形。
注意:
给定的火柴长度和在 0 到 10^9之间。
火柴数组的长度不超过15。
详见:https://leetcode.com/problems/matchsticks-to-square/description/
C++:
class Solution {
public:
bool makesquare(vector<int>& nums)
{
if (nums.empty() || nums.size() < 4)
{
return false;
}
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum % 4 != 0)
{
return false;
}
vector<int> sums(4, 0);
sort(nums.rbegin(), nums.rend());
return helper(nums, sums, 0, sum / 4);
}
bool helper(vector<int>& nums, vector<int>& sums, int pos, int target) {
if (pos >= nums.size())
{
return sums[0] == target && sums[1] == target && sums[2] == target;
}
for (int i = 0; i < 4; ++i) {
if (sums[i] + nums[pos] > target)
{
continue;
}
sums[i] += nums[pos];
if (helper(nums, sums, pos + 1, target))
{
return true;
}
sums[i] -= nums[pos];
}
return false;
}
};
参考:https://www.cnblogs.com/grandyang/p/6238425.html
473 Matchsticks to Square 火柴拼正方形的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)
Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...
- Java实现 LeetCode 473 火柴拼正方形
473. 火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到 ...
- leetcode 473. 火柴拼正方形(DFS,回溯)
题目链接 473. 火柴拼正方形 题意 给定一串数,判断这串数字能不能拼接成为正方形 思路 DFS,但是不能每次从从序列开始往下搜索,因为这样无法做到四个边覆盖不同位置的值,比如输入是(5,5,5,5 ...
- Leetcode 473.火柴拼正方形
火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为 ...
- [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- 473. Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- 【leetcode】473. Matchsticks to Square
题目如下: 解题思路:居然把卖火柴的小女孩都搬出来了.题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等.首先我们可以很容易的求出每个子数组的和应该是avg = sum(n ...
随机推荐
- poj2481 Cows
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- 设计模式 之代理(Proxy)模式
为什么这里要定义代理呢?所谓代理代理,当然就是你不想做的事.找别人去做,这就是代理.所以,当你写代码的时候.你想保持类的简单性.重用性.你就能够把事件尽量都交给其他类去做.自己仅仅管做好自己的事.也就 ...
- 【甘道夫】并行化频繁模式挖掘算法FP Growth及其在Mahout下的命令使用
今天调研了并行化频繁模式挖掘算法PFP Growth及其在Mahout下的命令使用,简单记录下试验结果,供以后查阅: 环境:Jdk1.7 + Hadoop2.2.0单机伪集群 + Mahout0.6 ...
- Web安全漏洞及攻击
背景介绍 先说一个在互联网上常见,但是普通人又不太理解的东西--“验证码”. 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell ...
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
wpf 导出Excel 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...
- [CSAPP]Bufbomb实验报告
Bufbomb实验报告 实验分析: level 0-3从test開始制运行,通过函数getbuf向外界读取一串内容(buf). Level 4 是通过參数-n,程序运行testn函数,调用getbuf ...
- PHP出现Warning: A non-numeric value encountered问题的原因及解决方法
本文介绍php出现Warning: A non-numeric value encountered问题,用实例分析出现这种错误的原因,并提供避免及解决问题的方法. <?php error_rep ...
- 3.myeclipse 8.5 m1 注册码
为了能在eclipse 中方便的使用uml,尝试了多次安装各种uml插件未果,myeclipse 自带uml插件,但是要注册啊,要破解啊!!! user:baiduzhidaopassword:oLR ...
- 检查 统计 异常 通信 time_wait
[root@hadoop1 conf]# netstat -n | grep -v 127.0.0.1 | grep -v :3306 | grep TIME_WAIT | sort -k 5n | ...
- Oracle 表的创建 及相关參数
1. 创建表完整语法 CREATE TABLE [schema.]table (column datatype [, column datatype] - ) [TABLESPACE tablespa ...