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.

题意:

给定一个数组,问该数组能否分成两个非空子集,使得这两个非空子集的元素之和相同

思路:

观察例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分割数组的和相同子集的更多相关文章

  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. 416 Partition Equal Subset Sum 分割相同子集和

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

  5. LC 416. Partition Equal Subset Sum

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

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

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

  7. 【leetcode】416. Partition Equal Subset Sum

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

  8. 416. Partition Equal Subset Sum

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

  9. [刷题] 416 Partition Equal Subset Sum

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

随机推荐

  1. 【Linux_Unix系统编程】chapter5 深入探究文件IO

    Chapter5 深入探究文件I/O 本章节将介绍另一个与文件操作相关的系统调用:多用途的fcntl(),并展示其应用之一读取和设置打开文件的状态标志. 5.1 原子操作和竞争条件 所有系统调用都是以 ...

  2. css sprite实例

    css sprite直译过来就是CSS精灵.通常被解释为“CSS图像拼合”或“CSS贴图定位”.本文章向码农们介绍css sprite使用方法和基本使用实例,需要的码农可以参考一下. 一.什么是css ...

  3. QQ去除聊天框广告详解——2016.9 版

    QQ聊天框广告很烦人,百度出来的一些方法早已过时,下面是博主整理出来的方法,供各位同学参考. 1.按键盘上的 Win+R 快捷键打开运行框,然后复制并粘贴 Application Data\Tence ...

  4. webpack(3)--Output

    Output output配置如何输出最终想要的代码,output是一个object里面包含一系列配置. 1. filename output.filename配置输出文件的名称,为string类型, ...

  5. Executor框架(四)周期/延时任务ScheduleThreadPoolExecutor

    ScheduledThreadPoolExecutor 介绍   ScheduledThreadPoolExecutor 是一个可以实现定时任务的 ThreadPoolExecutor(线程池).比 ...

  6. jquery knob旋钮插件

    <!DOCTYPE html> <html> <head> <title>jQuery Knob 尝试</title> <script ...

  7. Linux 各类设置、配置、使用技巧参考,Linux使用集锦

    ========== 参考格式 (新增记录时,复制粘贴在下)============= [日期]: <标题> 参考链接ref1: 参考链接ref2: 正文: ========== 参考格式 ...

  8. PY3 周总结 第一周

    1. Python的三大特点是什么? 答:解释型.动态类型(运行期间才做数据类型检查).强类型定义(一个变量只能存储一种类型的数据).[See More] 2. 应该使用Python2 还是 Pyth ...

  9. MYSQL三大范式

    第一范式:确保每列的原子性. 第一范式是最基本的范式. 数据库表中的字段都是单一属性的,不可再分. 只要是关系数据库都满足第一范式 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子 ...

  10. 基于OpenGL编写一个简易的2D渲染框架-02 搭建OpenGL环境

    由于没有使用GLFW库,接下来得费一番功夫. 阅读这篇文章前请看一下这个网页:https://learnopengl-cn.github.io/01%20Getting%20started/02%20 ...