一开始想求所有结果为target的组合来着,但是所选元素不能重叠。用这个递归思想很简单,分成四个桶,每次把元素放在任意一个桶里面,最后如果四个桶相等就可以放进去,有一个地方可以剪枝,假如任意一个桶的元素和大于了target果断return。另一个优化的点在于,如果要求能不能也就是找到一个就可以的话,那么从大到小的找可以减少回溯的次数,否则会超时。学会了一个sort函数从大到小排序的新方法。sort(nums.rbegin(),nums.rend()); leetcode不能用sort(,,cmp)的那种方法不知道为什么。

#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
vector<int>ans;
vector<vector<int>>res;
vector<bool>used;
bool cmp(int a, int b)
{
return a > b;
}
bool permutation(vector<int>nums, int index, int n,int a,int b,int c,int d)
{
if (a>n/||b>n/||c>n/||d>n/)
return false;
if (index==nums.size()&&a==b&&c==d&&b==c)
{
return true;
}
return permutation(nums, index + , n, a + nums[index], b, c, d) || permutation(nums, index + , n, a, b + nums[index], c, d) || permutation(nums, index + , n, a, b, c + nums[index], d) || permutation(nums, index + , n, a, b, c, d + nums[index]);
}
public:
bool makesquare(vector<int>& nums) {
if (nums.size() == )
return false;
int sum=;
int i;
sort(nums.rbegin(),nums.rend());
for (i = ; i < nums.size(); i++)
{
sum += nums[i];
}
if (sum % != ||nums[nums.size()-]>sum/)
return false;
return permutation(nums, , sum,,,,);
}
};

leetcode473 Matchsticks to Square的更多相关文章

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

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

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

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

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

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

  4. Leetcode: Matchsticks to Square && Grammar: reverse an primative array

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

  5. 473. 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 "473. Matchsticks to Square"

    A trickier DFS, with a little bit complex recursion param tweak, and what's more important is prunin ...

  8. 473 Matchsticks to Square 火柴拼正方形

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

  9. 【leetcode】473. Matchsticks to Square

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

随机推荐

  1. POJ3628:Bookshelf 2【01背包】

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  2. PAT甲级1075 PAT Judge

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805393241260032 题意: 有m次OJ提交记录,总共有k道 ...

  3. SET ARITHABORT {ON | OFF}讲解

    SET ARITHABORT {ON | OFF} 在查询处理过程中如果出现溢出错误或把零作为除数则查询处理是否该终止如 果为ON 则表示终止查询如果为OFF 则表示返回一个警告信息对于进行算术运 算 ...

  4. [No0000F1]js获取喜马拉雅和荔枝FM电台专辑音频

    荔枝FM小书签.txt javascript: (function() { if ($('#down_url')) { $('#down_url').remove(); }; $(document.b ...

  5. 一个生产可用的mysql参数文件my.cnf

    [client]#客户端选项设置#设置客户端和连接字符集default_character_set = utf8port = 3306socket = /opt/mysql-5.6.24/tmp/my ...

  6. Chap4:区块链的应用技术[《区块链中文词典》维京&甲子]

  7. [dpdk] service core

    dpdk 17.11 增加了一组新的API,serivce core 如命名,就是用一组core跑service函数. 我自己的测试程序如下: https://github.com/tony-caot ...

  8. py 正则表达式 List的使用, cxfreeze打包

    从index.html当做检索出压缩文件,index.html的内容如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN& ...

  9. SpringBoot 使用RedisTemplate操作Redis

    新版: import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.T ...

  10. MySQL加锁处理分析(转)

    add by zhj: 非常棒的一篇文章,是我见过的讲加锁最棒最详细的文章了.之前听过网易的<MySQL微专业>,里面的课程讲的也很好,但锁这块讲的跟 这篇文章相比,还是有差距的.网易&l ...