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. Boss Group Worker Group NioEventLoopGroup

    宜人贷蜂巢API网关技术解密之Netty使用实践 - honeycomb2017的博客 - CSDN博客 https://blog.csdn.net/honeycomb2017/article/det ...

  2. [LeetCode] 9.Palindrome Number - Swift

    Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...

  3. Spring Data CrudRepository增删改查方法(八)

    CrudRepository   的主要方法 long count(); boolean exists(Integer arg0); <S extends StudentPO> S sav ...

  4. 前端基础之BOM和DOM和三个小示例(计时器、搜索框、select联动)

    一.BOM和DOM JavaScript分为 ECMAScript,DOM,BOM. BOM(Browser Object Model)是指浏览器对象模型,它使 JavaScript 有能力与浏览器进 ...

  5. ansible(2)

    一.ansible模块(yum.pip.service.conr.user.group) 上篇中我们已经学了ansible的几个模块,接下来再来学习几个,那么你是否知道ansible一共有多少模块呢? ...

  6. redis安装使用教程

    一:安装redis 1.下载redis并安装 $wget http://redis.googlecode.com/files/redis-2.2.10.tar.gz $tar zvxf redis-2 ...

  7. C# 编写 TensorFlow 人工智能应用

    TensorFlowSharp入门使用C#编写TensorFlow人工智能应用学习. TensorFlow简单介绍 TensorFlow 是谷歌的第二代机器学习系统,按照谷歌所说,在某些基准测试中,T ...

  8. java script 的工具

    1.Jsbeautifier 这个微型的美化器可以重新调整 bookmarklet 和丑陋的JavaScript的格式和缩进,也可以对使用流行的 Dean Edward 的 Packer 打包的脚本进 ...

  9. PAT 1063 Set Similarity[比较]

    1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be N ...

  10. [golang note] 包和导入

    package的作用        √ package是golang最基本的分发单位和工程管理中依赖关系的体现.        √ 每个golang源代码文件开头都拥有一个package声明,表示该g ...