还记得童话《卖火柴的小女孩》吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法。不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到。
输入为小女孩拥有火柴的数目,每根火柴用其长度表示。输出即为是否能用所有的火柴拼成正方形。
示例 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 火柴拼正方形的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  2. Java实现 LeetCode 473 火柴拼正方形

    473. 火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到 ...

  3. leetcode 473. 火柴拼正方形(DFS,回溯)

    题目链接 473. 火柴拼正方形 题意 给定一串数,判断这串数字能不能拼接成为正方形 思路 DFS,但是不能每次从从序列开始往下搜索,因为这样无法做到四个边覆盖不同位置的值,比如输入是(5,5,5,5 ...

  4. Leetcode 473.火柴拼正方形

    火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为 ...

  5. [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  6. 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  7. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  8. 473. Matchsticks to Square

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  9. 【leetcode】473. Matchsticks to Square

    题目如下: 解题思路:居然把卖火柴的小女孩都搬出来了.题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等.首先我们可以很容易的求出每个子数组的和应该是avg = sum(n ...

随机推荐

  1. oracle统计信息

    手工刷ORACLE统计信息  select count(1) from LOG_TRX_DETAIL;  select * from user_tab_statistics where table_n ...

  2. Random Forest 与 GBDT 的异同

    曾经在看用RF和GBDT的时候,以为是非常相似的两个算法,都是属于集成算法,可是细致研究之后,发现他们根本全然不同. 以下总结基本的一些不同点 Random Forest: bagging (你懂得. ...

  3. axis2开发webservice之编写Axis2模块(Module)

    axis2中的模块化开发.能够让开发者自由的加入自己所需的模块.提高开发效率,减少开发的难度. Axis2能够通过模块(Module)进行扩展. Axis2模块至少须要有两个类,这两个类分别实现了Mo ...

  4. MongoDB使用入门

    1.MongoDB的安装 步骤一:下载MongoDB 下载安装包:http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.4.tgz 步骤二:设置 ...

  5. 最简单的基于FFmpeg的移动端样例:IOS 视频转码器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  6. Linux服务基础命令

    ---恢复内容开始--- 1简介: Linux的网络功能相当强悍,一时之间我们无法了解所有的文阿罗命令,在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig     查询,设置网卡和i ...

  7. NYOJ110 剑客决斗

    剑客决斗 来源:Polish Olympiad in Informatics(波兰信息学奥林匹克竞赛) 时间限制:5000 ms  |  内存限制:65535 KB 难度:5   描述 在路易十三和红 ...

  8. memmove 和 memcopy

    1.memmove 函数原型:void *memmove(void *dest, const void *source, size_t count) 返回值说明:返回指向dest的void *指针 参 ...

  9. hdfs对namenode format 之后 应该首先检查内存消耗情况,以判断是否支持开启yarn

    http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-common/yarn-default.xml  3.0.0 yarn.sc ...

  10. 内存溢出-jvisualvm排查问题

    先来一段能够内存溢出的程序 public static void main(String[] args) { List<Object> list = new ArrayList<&g ...