Leetcode 416. Partition Equal Subset Sum
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.
本题用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的更多相关文章
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
- 【leetcode】416. Partition Equal Subset Sum
题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...
- 416. Partition Equal Subset Sum
题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...
- 416 Partition Equal Subset Sum 分割相同子集和
详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...
- LC 416. Partition Equal Subset Sum
题目 Given a non-empty array containing only positive integers, find if the array can be partitioned i ...
- [刷题] 416 Partition Equal Subset Sum
要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...
随机推荐
- 浅析jQuery删除节点的三个方法
jQuery提供了三种删除节点的方法,即remove(),detach()和empty().测试所用HTML代码:[html] view plaincopy<p title="选择你最 ...
- codevs 1033 蚯蚓的游戏问题
Description 在一块梯形田地上,一群蚯蚓在做收集食物游戏.蚯蚓们把梯形田地上的食物堆积整理如下: a(1,1) a(1,2)…a(1,m) a(2,1) a(2,2) a(2,3)…a ...
- [转]Class 'Think\Log' not found
转自:http://www.thinkphp.cn/topic/26815.html 解决偶尔出现 Class 'Think\Log' not found 的奇葩问题(并非每次必现,偶尔删除缓存可以解 ...
- distributed caching for .net applications
distributed caching for .net applications fast, scalable distributed caching with meaningful perform ...
- 浅谈设计模式--建造器模式(Builder Pattern)
建造器模式,是于创建带有大量参数的对象,并避免因参数数量多而产生的一些问题(如状态不一致-JavaBean的setter模式). 如果参数多且有些是必须初始化的,有些是不一定需要初始化的时候,创建对象 ...
- CoreBluetooth——IOS蓝牙4.0使用心得
原文链接:http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&a ...
- 一起写一个JSON解析器
[本篇博文会介绍JSON解析的原理与实现,并一步一步写出来一个简单但实用的JSON解析器,项目地址:SimpleJSON.希望通过这篇博文,能让我们以后与JSON打交道时更加得心应手.由于个人水平有限 ...
- WebBrowser与IE的关系,如何设置WebBrowser工作在IE9、10、11模式下?
Web Browser Control – Specifying the IE Version http://www.west-wind.com/weblog/posts/2011/May/21/We ...
- js的一些冷门的用法
1.delete 2.void 0 3.>>> 4.>>0 字符串转为数字 5.[] == ![] 6.
- ASP.NET 问题集锦
[1]解决错误:从客户端(Content="<p>测试</p>")中检测到有潜在危险的 Request.Form 值 .NetFrameWork ...