一开始想求所有结果为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. Glufster挂载失败Mount failed. Please check the log file for more details解决办法

    设置两台glusterfs服务器主机名分别为gfs1,gfs2 设置好glusterfs挂载不成功提示如下 Mount failed. Please check the log file for mo ...

  2. Zabbix使用grafana展示图形

    系统环境查看 官网下载grafana wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.0.1-1. ...

  3. F#周报2018年第48期

    新闻 F#2018年圣诞日历 Mac上的Visual Studio 2017新版本7.7 Rider 2018.3将引入远程调试功能 Visual Studio 2017新版本15.9.3 视频及幻灯 ...

  4. RabbitMQ消息可靠性

    那些情况会失败 网络问题有很多原因出发失败.防火墙也可能会中断Idle连接,网络失败不是很快确定的. 硬件和软件也会导致系统崩溃.客户端软件保持运行,而逻辑错误也可能会导致channel和connec ...

  5. [dpdk][kni] dpdk kernel network interface

    文档:https://doc.dpdk.org/guides/prog_guide/kernel_nic_interface.html 摘要: The KNI kernel loadable modu ...

  6. iOS将excel转plist

    iOS将excel转plist 先把excel用Numbers打开,转换成CSV,然后新建一个工程,写下面的代码: - (void)viewDidLoad { [super viewDidLoad]; ...

  7. web.xml中 /和/*的区别

    < url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url< url-patter ...

  8. Javascript 面向对象编程(一):封装 作者:yuan一峰

    学习Javascript,最难的地方是什么? 我觉得,Object(对象)最难.因为Javascript的Object模型很独特,和其他语言都不一样,初学者不容易掌握. 下面就是我的学习笔记,希望对大 ...

  9. 洛谷P3224 永无乡 [HNOI2012] 线段树/splay/treap

    正解:线段树合并 解题报告: 传送门! 这题也是有很多解法,eg:splay,treap,... 然而我都不会我会学的QAQ! 反正今天就只讲下线段树合并怎么做QAQ 首先看到这样子的说第k重要的是什 ...

  10. collection my favoriate websites

    802.1x认证客户端linux版安装 http://blog.csdn.net/StarChunli/article/details/50918121 在U盘上安装kali linux http:/ ...