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[j] = dp[j] || dp [j - n];

Code

class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = 0;
for (int n : nums)
sum += n;
if (sum % 2 != 0)
return false;
sum /= 2;
return subsetsum(nums, sum);
}
bool subsetsum(vector<int>& nums, int sum) {
int dp[sum + 1] = {false};
dp[0] = true;
for (int i = 0; i < nums.size(); i++) {
for (int j = sum; j >= nums[i]; j--) {
dp[j] = dp[j] || dp[j - nums[i]];
}
}
return dp[sum];
}
};

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

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

  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. property:get、set

    property属性:自动调用get.set方法 每次调用隐藏的数据,都用get和set方法写的字符太长,就用property解决. 方法一:使用property()函数升级get.set方法   源 ...

  2. SQL Server中SCAN 和SEEK的区别

    SQL Server中SCAN 和SEEK的区别 SQL SERVER使用扫描(scan)和查找(seek)这两种算法从数据表和索引中读取数据.这两种算法构成了查询的基础,几乎无处不在.Scan会扫描 ...

  3. 划分LUN

    划分LUN http://mp.weixin.qq.com/s?__biz=MzAwNzU3NzQ0MA==&mid=209842199&idx=1&sn=7d77fdf7a8 ...

  4. SQLServer和MySQL job和 event定时器的差别

    SQLServer和MySQL job和 event定时器的差别

  5. MYSQL流程图

    MYSQL流程图

  6. (转)Kangle配置文件

    kangle配置文件 (重定向自Kangle配置文件) 目录 [隐藏] 1配置文件介绍 2重新加载配置文件 3config 3.1request和response(配置访问控制) 3.2listen( ...

  7. ZOJ:2833 Friendship(并查集+哈希)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2833 A friend is like a flower, a rose ...

  8. Apache下设置网站目录的访问权限

    禁止用户对某一个目录及目录下文件的访问,仅允许本地访问 <Directory "/wwwroot/cert/"> Require local </Director ...

  9. 深入浅出TCP之listen

    原文:http://blog.chinaunix.net/uid-29075379-id-3858844.html int listen(int fd, int backlog); 有几个概念需要在开 ...

  10. pycharm跳到指定的行

    ctrl+g  弹出一个框,输入要跳的行数 ctrl+home  光标移到第一行 ctrl+end 光标移到最后一行