[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], ...
随机推荐
- [UE4]添加射击的准心
其实就是创建一个UI Widget,在UI Widget中添加一个准心图片(png)格式,准心图片设置为屏幕居中对齐,然后在自定义的GameMode中把这个UI Widget添加到视图中.
- Javascript框架
网易开源框架http://www.oschina.net/p/nej http://www.linuxeden.com/html/develop/20120716/127404.html 16 款最流 ...
- java编译器知识
代码编译器: 代码: 编译就是讲一种代码编译成计算机可以理解的指令. ================================================================= ...
- 手贱!使用django,在数据库直接删除了表
莫名其妙的错误. 删除了migreation文件,并且更换了数据库. 1.直接makemigrations + migrate error: no change ?? WTF 2.makemi ...
- OpenACC kernels
▶ 使用 kernels 导语并行化 for 循环 ● 一重循环 #include <stdio.h> #include <time.h> #include <opena ...
- zabbix3.4.7 饼图显示问题
最近安装了zabbix3.4.7,发现系统自带Template OS Linux模版饼图(Pie)有两个问题: Total disk space on / 显示为 no data,也就是没有数据: 把 ...
- THREE.JS 场景世界坐标和平面二维坐标互转
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 巧用JLINK来实现nrf51822的蓝牙设备流水号
项目需求:在蓝牙广播的时候名字为 SN_设备流水号(如SN_00000001). 我们可以在原来的代码中进行一下修改和增加 ; 备注这个地址0X0001B160根据自己具体情况来设定 /**@brie ...
- ajax 遍历json一维数组
$.each(data,function(index,value){}data必须是Object类型index是数组的下标value可以是一个对象 function myonclick() { var ...
- eclipse包层级显示和工作空间显示
本文两件事:设置包层级显示.设置工程的工作空间显示 一.各package包分层显示 平铺显示,实在不方便开发!也不方便查看工程包的层级结构,如下: 更换成层级显示: 二.工作空间显示 包用来区分类,工 ...