一开始想求所有结果为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. day2:day1作业 字符编码

    1,使用while循环输出1,2,3,4,5,6,8,9 # 使用while循环输出1,2,3,4,5,6,8,9 num = 1 while num <= 10: if num != 7: p ...

  2. ACM:油田(Oil Deposits,UVa 572)

    /* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  3. Page7:能控性、能观性及其判据和对偶原理(2)[Linear System Theory]

    内容包含连续时间时变系统的能控性和能观测性判据,离散时间线性系统的能控性和能观测性判据,以及对偶原理

  4. Chap3:文件系统中跳转[The Linux Command Line]

    1 introduce the following commands pwd - Print name of current working directory cd-Change directory ...

  5. 自己写的JdbcUtils小工具-----得到Connection对象

    Properties文件中存放键值对------(可看对Properties文件的解析) static代码块是在构造函数之前执行的,而且只执行一次,即类首次加载时. 也就是只加载一次配置文件和加载数据 ...

  6. Blender 使用

    教程: 1.https://www.youtube.com/watch?v=N8-mE-165b8&index=7&list=PLE885296A496C3D38 快捷键: http: ...

  7. Cocos 2d TestCPP 学习

    Cocos 2d testcpp包含了大量的demo, 对于新手学习cocos引擎具有非常大的帮助.因为接下来的开发项目有可能会用到该引擎,所以希望可以利用自己的业余时间提前熟悉起来.该篇文章会记录自 ...

  8. C#查找字符串位置

    int i=zifuchuan.IndexOf(","); int n=(zifuchuan.SubString(i+1)).IndexOf(","); int ...

  9. SQL join的介绍

    学员表 SELECT * FROM tb_address; SELECT * FROM tb_student 1.JOIN关联两个表数据,将匹配数据展示,数据无匹配值则不展示 注释:INNER JOI ...

  10. Python开发【模块】:aiohttp(二)

    AIOHTTP 1.文件上传 ① 单个文件上传 服务端 async def post(self, request): reader = await request.multipart() # /!\ ...