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 ...
随机推荐
- Office EXCEL 如何设置最大行高
对于单个单元格行来说,行高必须在0-409之间 但是如果合并了两个单元格,则行高就扩展了一倍,不止409,而是两倍的409.
- python学习第一
#python学习day1#一.变量#变量命名规范:#驼峰命名法:AgeOfPlane#下划线命名(推荐):age_of_plane#变量格式同C/C++#注意:变量不以中文命名:变量不宜过长:变量因 ...
- 让Linq的OrderBy支持动态字段
使用linq的OrderBy,如果明确知道是哪个字段,当然很容易: IQueryable<User> userQuery = ...; userQuery.OrderBy(u => ...
- Educational Codeforces Round 17 D. Maximum path DP
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...
- css class嵌套
css 代码: <style> .chose_bonus { font-size:9px;width:400px;border: 2px solid #dddddd;margin-top: ...
- 什么叫强类型的DATASET
强类型DataSet,是指需要预先定义对应表的各个字段的属性和取值方式的数据集.对于所有这些属性都需要从DataSet, DataTable, DataRow继承,生成相应的用户自定义类.强类型的一个 ...
- android 代码优化:关闭输出日志
android关闭日志 我们在开发时,经常会输出各种日志来debug代码.但是等到应用发布的apk运行时不希望它输出日志. 关闭输出日志Log.v(),Log.i(),Log.w(),Log.v(), ...
- 【AHOI 2005】 约数研究
[题目链接] 点击打开链接 [算法] 要求M,显然可以通过约数个数定理从1..N暴力计算答案,然而n最大10^6,这个算法的时间复杂度是 O(N * sqrt(N))的,不能通过此题 因此我们换一种思 ...
- mysql数据恢复失败记录
今天遇到了MySQL有几个数据表空间丢失的问题,作为一个外行尝试好久没恢复成功,考虑到只是几个基础数据表,就删除数据表停止服务,删除ibd文件后再创新创建表解决了问题. 近期的一些事让我不像以前一样钻 ...
- cocos2d-x-2.1.5版本在Xcode4.2中首次编译ccimage Semantic Issue出错解决方法
// XXX: ios7 casting [str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) wi ...