[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.
题意:
给定一个数组,问该数组能否分成两个非空子集,使得这两个非空子集的元素之和相同
思路:
观察例1,sum=22, sum为偶数有可能partition两个子集
观察例2,sum=11,sum 为奇数不可能partition两个子集
这是一个背包问题,背包容量为数组中元素和的一半+1。这样只要看是否有元素正好填满背包即可。但每个元素只能用一次,所以在尝试放一个元素时还要避免对尝试放其他位置时对自己的影响。所以尝试放一个元素到背包的时候,要从容量最大的开始。
代码:
class Solution {
public boolean canPartition(int[] nums) {
int sum = 0;
for(int i = 0; i < nums.length; i++){
sum += nums[i];
}
if(sum % 2 != 0) return false;
sum = sum / 2;
int[]dp = new int[sum + 1];
dp[0] = 1;
for(int i = 0; i< nums.length; i++){
for(int j = sum; j>=nums[i]; j--){
dp[j] = dp[j]|dp[j-nums[i]];
}
}
return dp[sum] != 0;
}
}
[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 ...
- 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 ...
- 【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
要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...
随机推荐
- Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图
Maven 组件界面介绍 如上图标注 1 所示,为常用的 Maven 工具栏,其中最常用的有: 第一个按钮:Reimport All Maven Projects 表示根据 pom.xml 重新载入项 ...
- HTML5之viewport使用
好久都没更新博客了,最近一年转型移动端,当然网页端也得兼顾,慢慢写一写基本性的文章,多积累. 本期介绍下viewport的一些使用: 先看看viewport在页面中的样子: <meta name ...
- ORM( ORM查询13种方法3. 单表的双下划线的使用 4. 外键的方法 5. 多对多的方法 ,聚合,分组,F查询,Q查询,事务 )
必知必会13条 <1> all(): 查询所有结果 <2> get(**kwargs): 返回与所给筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或 ...
- javascript日期相减,求时间差
//计算时间差 var from_date = new Date(from_time); var end_date = new Date(end_time); var time_different = ...
- 3. HashMap和JSONObject用法
<%@page import="net.sf.json.JSONObject"%><%@page import="java.util.List" ...
- IdUDPServer 收到4次重复的数据
IdUDPServer1->Send(RemoteIP, LabeledEdit2->Text.ToInt(), InText, IndyTextEncoding_UTF8()); 我发给 ...
- VBA 打开带密码的文件
' 打开文件 ROSE 为只读密码 CHECK 为编辑密码 Set wb = Workbooks.Open(file, 0, True, , "ROSE", "CH ...
- VBA 操作 VBE
Introduction You can write code in VBA that reads or modifies other VBA projects, modules, or proced ...
- Reactjs 打包后 Tomcat 部署 404问题
配置web.xml <error-page> <error-code>404</error-code> <location>/index.html< ...
- VC 判断网络连接函数
IsNetworkAlive Bool IsNetworkAlive( _Out_ LPDWORD lpdwFlags ); Header Sensapi.h Library Sensapi.lib ...