LC 416. Partition Equal Subset Sum
题目
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Note:
- Each of the array element will not exceed 100.
- The array size will not exceed 200.
Example 1:
Input: [1, 5, 11, 5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: [1, 2, 3, 5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
参考答案
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = accumulate(nums.begin(),nums.end(),); //首先对数组求和
if( sum & ){ // 如果数组是个奇数,那么不成立,因为例如: 5 = 2+3
return false;
}
int half = sum / ;
std::sort(nums.begin(),nums.end(),std::greater<int>()); // 一定要加,否则time limit exceeded
return foo(nums,half,); // 将 half 作为输入
}
bool foo(vector<int>& nums, int half, int index){
for(int i = index ; i <nums.size() ; i++){ // 对于每一个数进行迭代
int h = half - nums[i]; // 对 half 扣除
if(h<) return false;
if(h==) return true;
// 如果上述条件都不满足,说明nums[i] 没办法满足条件,那么需要继续找下一个数,
20 // 即index = i+1。满足即true,不满足就退回到上层,在这一层找下一个数。
if(foo(nums,h,i+) == true) return true;
}
return false;
}
};
LC 416. Partition Equal Subset Sum的更多相关文章
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode 416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- 416. Partition Equal Subset Sum
题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
- 416 Partition Equal Subset Sum 分割相同子集和
详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...
- 【leetcode】416. Partition Equal Subset Sum
题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...
- [刷题] 416 Partition Equal Subset Sum
要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...
随机推荐
- c++ demo code
/* //多继承 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void s ...
- input输入框限制只能输入数字
js: function onlyNumber(event){ var keyCode = event.keyCode; if((keyCode<48&&keyC ...
- open suse tumbleweed安装记录
zypper install imagewriter cmake blender fontforge gimp digikam inkscape kontact pitivi smplayer si ...
- Chisel-LLDB命令插件,让调试更Easy
http://blog.cnbluebox.com/blog/2015/03/05/chisel/ LLDB 是一个有着 REPL 的特性和 C++ ,Python 插件的开源调试器.LLDB 绑定在 ...
- let和const关键字
一:let 关键字 1.作用: - 与var类似, 用于声明一个变量 2.特点 - 在块作用域内有效 - 不能重复声明 - 不会预处理, 不存在提升 3.应用 - 循环遍历加监听 - 使用let取代v ...
- JAVA基础知识|枚举
将代码中经常使用的常量,放在枚举中,是一个很好的编码习惯.便于统一修改,同时也增强代码的严谨和稳定性.当然,枚举的应用有很多,这里我只做一个简单的演示,以后看到有趣的使用,会慢慢丰富 package ...
- fatal: unable to access 'https://github.com/Homebrew/homebrew-core/'
LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54 安装curl "https://nodejs.org/dist/latest/node-${VE ...
- webpack打包---报错内存溢出javaScript heap out of memory
今天, npm run build打包时,又报内存溢出了.所以记录一下,之前查了博客有一些解释. “报错CALL_AND_RETRY_LAST Allocation failed - JavaScri ...
- err="etherbase address must be explicitly specified"
如果要初始化区块链的话就用创始区块 如果通过创世区块来初始化区块链的话,首先需要一个初始化区块链的json文件,如下. { "config": { "chainId& ...
- Python 基础学习的几个小例子
最近在研究动态脚本语言 Python , 出于对其语言精简度的喜欢及大学时期对 matlab 这样的数学领域语言的怀念, 再加上笔者是C++起家,Python中所涉及的del机制与特殊方法重载(类比于 ...