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.

Backpack problem: dp[i][j] means if the first i elements can sum up to value j

dp[i][j] = dp[i-1][j] || (j>=nums[i-1] && dp[i-1][j-nums[i-1]])

the result is if the half sum of the array can be summed up by elements in the array

 public class Solution {
public boolean canPartition(int[] nums) {
if (nums.length == 0) return true;
int volume = 0;
for (int num : nums) {
volume += num;
}
if (volume % 2 == 1) return false;
volume /= 2;
boolean[] dp = new boolean[volume+1];
dp[0] = true;
for (int i=1; i<=nums.length; i++) {
for (int j=volume; j>=0; j--) {
dp[j] = dp[j] || (j>=nums[i-1] && dp[j-nums[i-1]]);
}
}
return dp[volume];
}
}

Leetcode: Partition Equal Subset Sum的更多相关文章

  1. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  2. Leetcode ——Partition Equal Subset Sum

    Question Given a non-empty array containing only positive integers, find if the array can be partiti ...

  3. LeetCode—— Partition Equal Subset Sum

    Question Given a non-empty array containing only positive integers, find if the array can be partiti ...

  4. LN : leetcode 416 Partition Equal Subset Sum

    lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...

  5. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  6. [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  7. Leetcode 416. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

随机推荐

  1. iOS 上线被拒收集

    根据上线被拒的原因 自己 也在慢慢总结  希望对各位有所帮助 1)QQ 微信 等第三方平台 必须要做是否安装应用的检测

  2. Visio 2007/2010 左侧"形状"窗口管理

    Visio 2007/2010 左侧"形状"窗口管理 Visio 打开后,通常窗口左侧会有一个“形状”面板,我们可以方便地从中选择需要的形状.有时为了获得更大的版面空间或者不小心关 ...

  3. C# Textbox的ImeMode取值对中文输入法的影响 (转)

    摘自:http://blog.csdn.net/jhycjhyc/article/details/6578570 C# Textbox的ImeMode取值对中文输入法的影响           取值 ...

  4. 全浏览器收藏网站javascript

    function MyFavorite(sURL, sTitle) { var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != ...

  5. Error #include nested too deeply

    转载:http://blog.csdn.net/ysdaniel/article/details/7043395 出现 Error #include nested too deeply 原因是: 头文 ...

  6. Hibernate检索策略之延迟加载和立即加载

    延迟加载:延迟加载(lazy load懒加载)是当在真正需要数据时,才执行SQL语句进行查询.避免了无谓的性能开销. 延迟加载分类:  1.类级别的查询策略 2.一对多和多对多关联的查询策略 3.多对 ...

  7. js实现事件模型bind与trigger

    function Emitter() { this._listener = [];//_listener[自定义的事件名] = [所用执行的匿名函数1, 所用执行的匿名函数2] } //注册事件 Em ...

  8. js 时间格式化 -- 时间加减实现

    时间格式化的方法: Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.ge ...

  9. 学习Jquery

    早就听说了Jquery的大名,一直没有细心的学习一下,通过阅读收集的一些资料,感觉Jquery真的很强大.决定开始自己的学习Jquery之旅.在这里不是为大家讲解Jquery(深知水平有限),只是将自 ...

  10. Ajax提交后台中文乱码问题

    今天项目组同事反映,之前有一个正常的请求,今天突然后台获取参数的值出现了中文乱码,怀疑是之前更新jar包所致,笔者仔细想了想更新的内容,仿佛没有涉及到编码的变更啊,然后开始排查,首先后台加了强制以ut ...