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的更多相关文章

  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. UISearchBar 详解

    UISearchBar 详解   最近用到搜索功能.于是,经过不断的研究,终于,有点懂了. 那就来总结一下吧,好记性不如烂笔头! 搜索,无疑可以使用UISearchBar控件! 那就先了解一下UISe ...

  2. .Net Ajax跨域请求总结

    导语 之前写过一篇文章Ajax跨域请求COOKIE无法带上的解决办法,这两天正好好好的查了一下相关知识,做来总结一下 一.传统 ajax跨域访问是一个老问题了,解决方法很多,比较常用的是JSONP方法 ...

  3. MSSQL移除字符串两边的指定字符

    移除字符串左边的字符: CREATE FUNCTION [dbo].[RemoveLeftChar] ( @Expression varchar(max), @char varchar(4))RETU ...

  4. [genome shell]标题栏优化

    参考地址:https://wiki.archlinux.org/index.php/GNOME_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)#.E4.BB.8E.E5. ...

  5. Mustache 中的html转义问题处理

    避免在使用Mustache引擎是发生html字符转义 1,模板代码示例:    var xml= " <?xml version="1.0" encoding=&q ...

  6. OnePy--构建属于自己的量化回测框架

    本文主要记录我构建量化回测系统的学习历程. 被遗弃的项目:Chandlercjy/OnePy_Old 新更新中的项目:Chandlercjy/OnePy 目录 1. 那究竟应该学习哪种编程语言比较好呢 ...

  7. Period II---fzu1901(Next数组)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 给你一个字符串 s 求出所有满足s[i] == s[i+p] ( 0 < i+p < len ...

  8. new Option() 创建一个option标签

    //add() 方法用于向 <select> 添加一个 <option> 元素. //new Option() 创建一个option标签 school.add(new Opti ...

  9. Linux命令(基础1)

    一  命令的基本构成 (PS:Linux发行版本命令大概有200多个,熟练掌握个百八的就行了,其余的有个大概了解) 命令体 选项 参数(对象) ls -l /var 1.1参数:文件 文件类型: d ...

  10. Redis在实际项目中的一应用场景

    1.在游戏的等级排名,可以将用户信息放入到redis的有序集合中,然后取得相应的排名,不用自己写代码去排序. 2.利用rediss的数据特性的自增,自减属性,可以将项目中的一些列入阅读数,点赞数放入到 ...