leetcode473 Matchsticks to Square
一开始想求所有结果为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的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)
Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...
- [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- 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 ...
- 473. Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- LeetCode "473. Matchsticks to Square"
A trickier DFS, with a little bit complex recursion param tweak, and what's more important is prunin ...
- 473 Matchsticks to Square 火柴拼正方形
还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到.输入为小女孩拥有火柴的 ...
- 【leetcode】473. Matchsticks to Square
题目如下: 解题思路:居然把卖火柴的小女孩都搬出来了.题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等.首先我们可以很容易的求出每个子数组的和应该是avg = sum(n ...
随机推荐
- vue axios跨域请求,代理设置
在config下的index.js中的dev下的 proxyTable{}中设置代理 proxyTable: { '/api': { target: 'http://10.0.100.7:8081', ...
- 浅谈原生JavaScript实现remove()和recover()
利用原生JavaScript实现: 1.remove(selectors)删除指定的一个或一组元素. 2.recover(selectors)恢复刚才删除的元素. function remove(se ...
- /usr/bin/ld: 找不到 -lmsc----解决方案
系统的默认搜索依赖库路径为,/usr/local/lib 在camkelists.txt文件中对可执行文件链接libmsc.so add_executable(iat_publish src/iat_ ...
- TensorRT优化过程中的dropout问题
使用tensorRT之前,你一定要注意你的网络结构是否能够得到trt的支持,无论是CNN还是RNN都会有trt的操作. 例如:tf.nn.dropout(features, keep_prob),tr ...
- static的含义
static的含义:(1)设置变量的存储域,函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍持上次的值:(2)限制变量的作用域,在模块 ...
- [knowledge][模式匹配] 字符匹配/模式匹配 正则表达式 自动机
字符串 T = abcabaabcabac,字符串 P = abaa,判断P是否是T的子串,就是字符串匹配问题了,T 叫做文本(Text) ,P 叫做模式(Pattern),所以正确描述是,找出所有在 ...
- Python生成器表达式
https://www.cnblogs.com/liu-shuai/p/6098218.html 简介: 生成器表达式并不真正的创建数字列表,而是返回一个生成器对象,此对象在每次计算出一个条目后,把这 ...
- 清理solaris /var/mail/下的邮件文件
我服务器上/var/mail下的各个用户的邮件日志非常大,占用空间已经有95%了,我想清除掉,是否可以直接删除/var/mail的各个日志??删除后系统是否可以自动生成? 应该可以直接删除/var/m ...
- LeetCode 104 Maximum Depth of Binary Tree 解题报告
题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- liteide 去除go程序cmd窗口
http://blog.csdn.net/aqtata/article/details/53389261