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

    一 准备Ruby环境 1 我们首先须要安装Ruby.从站点下载Ruby 上下载Ruby最新版和对应的DevKit. 我下载的是Ruby 2.1.4 (x64)和DevKit-mingw64-6 .注意 ...

  2. AspNetPager真假分页对照实例

    从開始学习BS已经有一段时间了. 对于BS的设计,都是进行的网页设计,当中包含从数据库中取出来的数据.显示在页面上.曾经在CS中,都是使用GridView等表格控件进行显示,因为数据小.并且右側又有滚 ...

  3. Linux - Ubuntu中文输入法安装(Ubuntu 12.04)

    Ubuntu中文输入法安装(Ubuntu 12.04) 本文地址:http://blog.csdn.net/caroline_wendy Ubuntu作为Linux常见的操作系统,是须要熟练使用的. ...

  4. 【转载】SOAP协议介绍

    SOAP是用在分散或分布的环境中交换信息的简单的协议,它是一个基于XML的协议,包括三个部分:封装定义了一个描述消息中包含什么内容以及如何处理它们的框架,编码规则用于表示应用程序定义的数据类型的实例, ...

  5. C语言细节笔记1

    /******************************************************************************* ——笔记 1. 函数申明的书写. 可以 ...

  6. Oracle学习(18)【DBA向】:分布式数据库

    分布式数据库 什么是分布数据库? l数据物理上被存放在网络的多个节点上,逻辑上是一个总体. 分布式数据库的独立性 l分布数据的独立性指用户不必关心数据怎样切割和存储,仅仅需关心他须要什么数据. Ora ...

  7. 用bis和bic实现位级操作

    20世纪70年代末至80年代末,DigitalEquipment的VAX计算机是一种非常流行的机型.它没有布尔运算AND和OR指令,仅仅有bis(位设置)和bic(位清除)这两种指令.两种指令的输入都 ...

  8. Blocks实现代理传值

    一.RootViewController: #import "RootViewController.h" #import "SecondViewController.h& ...

  9. aspx后台引用不到服务器控件

    从其他地方拷贝的页面到自己的项目,后台CS代码引用hidden时,提示找不到,百度,发现可能是网站项目和Web应用程序的区别,右键aspx转化为Web应用程序即可.

  10. Okapi BM25 (BM stands for Best Matching)

    Okapi BM25 - Wikipedia https://en.wikipedia.org/wiki/Okapi_BM25 In information retrieval, Okapi BM25 ...