LeetCode—— Partition Equal Subset Sum
Question
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.
Solution
动态规划,这其实是一个0-1背包问题,dp[i][j],i表示用第0~i个数是否可以组成值为j,dp[0][0] = true.
Code
class Solution {
public:
bool canPartition(vector<int>& nums) {
int total = accumulate(nums.begin(), nums.end(), 0), target = total >> 1;
vector<vector<bool> > dp(nums.size() + 1, vector<bool>(target + 1, false));
if (total & 1)
return false;
for (int j = 0; j <= nums.size(); j++) {
dp[j][0] = true;
}
for (int i = 1; i <= target; i++)
dp[0][i] = false;
for (int i = 1; i <= nums.size(); i++) {
for (int j = 1; j <= target; j++) {
dp[i][j] = dp[i - 1][j];
if (j >= nums[i - 1])
dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];
}
}
return dp[nums.size()][target];
}
};
LeetCode—— Partition Equal Subset Sum的更多相关文章
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode: Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode ——Partition Equal Subset Sum
Question Given a non-empty array containing only positive integers, find if the array can be partiti ...
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- 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 ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
随机推荐
- andriod的apk文件相关的编译反编译工具
1.smali-1.2.6.jar 用途:.smali文件 转成 classes.dex文件 说明:.smali文件,类似于.class文件,可以用普通文本编辑器查看和修改. 用法举例:命令行:jav ...
- Apache Kafka源码分析 – Controller
https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Controller+Internalshttps://cwiki.apache.org ...
- Android Studio升级后报 method not found: 'runProguard'的错误
今天升级了下Android Studio,然后发现更新gradle,然后在sync项目的时候总是报 method not found: 'runProguard'的错误 找了很多发现不对. 最后解决 ...
- Jumpserver使用
堡垒机介绍 在一个特定网络环境下,为了保障网络和数据不受外界入侵和破坏,而运用各种技术手段实时收集和监控网络环境中每一个组成部分的系统状态.安全事件.网络活动,以便集中报警.及时处理及审计定责. 我们 ...
- JAVA—List集合总结
List接口总结: List接口是Collection接口的子接口,从其名称可以看出,是一个元素有序(并不是按大小排序,具有顺序索引,类似于数组),默认按照元素的添加顺序设置元素的索引,List和Se ...
- 使用LoadRunner的Web(HTTP/HTML)协议录制手机app脚本
一.打开HP Virtual User Generator,创建虚拟用户脚本,选择Web(HTTP/HTML)协议:
- Git 使用vi或vim
1.vi & vim 有两种工作模式: (1) 命令模式:接受.执行 vi & vim 操作命令的模式,打开文件后的默认模式: (2) 编辑模式:对打开的文件内容进行 增.删.改 操作 ...
- Pandas -- SettingwithCopyWarning 原理和解决方案(转)
本文对产生 SettingwithCopyWarning 的原因以及解决方案,做了详细解说. 详见: https://www.jianshu.com/p/72274ccb647a
- PAT 1116 Come on! Let's C [简单]
1116 Come on! Let's C (20 分) "Let's C" is a popular and fun programming contest hosted by ...
- 持续(集成-->交付-->部署)
软件的开发工作的大致流程 编码 -> 构建 -> 集成 -> 测试 -> 交付 -> 部署 由上图可知「持续集成(Continuous Integration)」.「持续 ...