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:

  1. Each of the array element will not exceed 100.
  2. 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.

本题用DP来解。DP[i]表示是不是存在一个subset使得sum等于i.

 class Solution(object):
def canPartition(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
s = sum(nums) if s % 2 == 1: return False
target = s/2
dp = [False]*(target+1)
dp[0] = True for i in range(len(nums)):
for j in range(target, nums[i] - 1, -1):
dp[j] = dp[j] or dp[j - nums[i]] return dp[target]

注意这L15中的循环必须是反过来的。如果取正向的循环,假设num=3,这个for循环会把所有的dp[3*n]都写成true。这相当于一个数字可以使用任意多次,这并不符合题意。

用类似的方法还可以解决另外一个问题:给定一个list of positive integers和一个target. 是否存在一个subset使得sum(subset) = target?

def subsetSum(nums, target):
s = sum(nums)
if s < target: return False
dp = [False] * (target + 1)
dp[0] = True for i in range(len(nums)):
for j in range(target, nums[i] - 1, -1):
dp[j] = dp[j] or dp[j - nums[i]] return dp[target]

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

  1. LN : leetcode 416 Partition Equal Subset Sum

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

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

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

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

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

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

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

  5. 【leetcode】416. Partition Equal Subset Sum

    题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...

  6. 416. Partition Equal Subset Sum

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

  7. 416 Partition Equal Subset Sum 分割相同子集和

    详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...

  8. LC 416. Partition Equal Subset Sum

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

  9. [刷题] 416 Partition Equal Subset Sum

    要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...

随机推荐

  1. 高端黑链SEO—恶意JS脚本注入访问伪随机域名

    摘要:我们的服务器又出入侵事故了.有客户的 html 网页底部被插入了一段 js 脚本,导致访客打开网页时被杀毒软件警告网站上有恶意代码.在黑链 SEO 中这是常见的手法,但奇特的地方就在于我们这次捕 ...

  2. codevs 2491 玉蟾宫

    codevs 2491 玉蟾宫 http://codevs.cn/problem/2491/ 题目描述 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉 ...

  3. 程序开发使用docker部署

    我们公司自己研发了一套 grand-line 系统,使用 docker 来部署项目. 我是第一批小白鼠,一开始网络差,build 一次要半个小时,连接进入 web shell 也很慢,部署一个微信项目 ...

  4. Windows Phone 8 显示当前项目的使用内存,最大峰值,最大内存上限

    public static class MemoryDiagnosticsHelper { public static bool isStart = false; static Popup popup ...

  5. team foundation server——网络代码管理工具

    像我们平时有时会莫名的弹出一个如下图所示的提示框,这个是什么呢?这个就是有人用team foundation server进行过代码管理的项目 那么team foundation server到底是什 ...

  6. background-size对background-position的影响

    CSS3中提出了background-size属性,该属性可以设置背景图片的大小,该属性的值设置为绝对数值或者百分比时对background-position没有任何影响,当设置为contain/co ...

  7. Vware Workstation pro 12|虚拟机

    Vmware是比较不错的PC虚拟化软件,vmware11+不在支持32的系统安装!体积比之前小了很多 VMware 12 官方中文页面 http://vmware.com/cn/products/wo ...

  8. Go加密解密之DES

    一.DES简介 DES(Data Encryption Standard)是对称加密算法,也就是加密和解密用相同的密钥.其入口参数有三个:key.data.mode.key为加密解密使用的密钥,dat ...

  9. webservice的常用注解

    定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...

  10. jquery渐渐的显示、隐藏效果

    <!DOCTYPE html> <html> <head> <meta charset="gb2312" /> <title& ...