分割等和子集

给定一个只包含正整数非空数组。是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。

注意:

  1. 每个数组中的元素不会超过 100
  2. 数组的大小不会超过 200

示例 1:

输入: [1, 5, 11, 5]

输出: true

解释: 数组可以分割成 [1, 5, 5] 和 [11].

示例 2:

输入: [1, 2, 3, 5]

输出: false

解释: 数组不能分割成两个元素和相等的子集.

用动态规划,即将每次遍历到的数的放入和不放入结果集合的状态都存起来。有点像背包问题,每次放或者不放一个数进去都会影响以后是否能放入未来的数。

 class Solution {
public boolean canPartition(int[] nums) {
int sum=0;
for(int num:nums){
sum+=num;
}
if((sum&1)==1) return false;
sum>>=1;
boolean[] dp=new boolean[sum+1];
dp[0]=true;
for(int j=0;j<nums.length;j++){
for(int i=sum;i>=nums[j];i--){
dp[i]=dp[i]||dp[i-nums[j]];
}
if(dp[sum]){
return true;
}
}
return dp[sum];
}
}

Leetcode 416.分割等和子集的更多相关文章

  1. Leetcode 416分割等和子集

    416. 分割等和子集 已知是个背包问题,由于可以等分为两部分,所以必定是个偶数. 一开始想到的是回溯法 bool helper(vector<int>&nums, int i, ...

  2. Java实现 LeetCode 416 分割等和子集

    416. 分割等和子集 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 注意: 每个数组中的元素不会超过 100 数组的大小不会超过 200 示例 1: ...

  3. [LeetCode]494. 目标和、416. 分割等和子集(0-1背包,DP)

    题目一 494. 目标和 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前 ...

  4. LeetCode:分割链表【86】

    LeetCode:分割链表[86] 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例 ...

  5. LeetCode 数组分割

    LeetCode 数组分割 LeetCode 数组怎么分割可以得到左右最大值的差值的最大 https://www.nowcoder.com/study/live/489/1/1 左右最值最大差 htt ...

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

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

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

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

  8. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. [Swift]LeetCode416. 分割等和子集 | Partition Equal Subset Sum

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

随机推荐

  1. Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...

  2. python os,sys模块的使用

    首先,os模块是用来与操作系统进行交互的模块,可以对操作系统上的一些东西进行操作 而sys是用来对解释器进行一些操作的 一.os os.getcwd() 获取当前工作目录,即当前python脚本工作的 ...

  3. js字符串、数组、时间、日期对象

    js对字符串.数组.日期的操作是在以后项目中频繁使用的操作,所以呢....所以大家看着办,其实并不难哈,就是有点无聊,我承认这是我百度的,哈哈哈哈 <!DOCTYPE html><h ...

  4. 微软Bot Framework文档中,关于Sign-in Card的一处代码错误及更正

    Bot Framework文档出错处网址:https://docs.botframework.com/en-us/csharp/builder/sdkreference/attachments.htm ...

  5. JDBC + SAP云平台 = 运行在云端的数据库应用

    在前一篇文章JPA + EclipseLink + SAP云平台 = 运行在云端的数据库应用我介绍了如何通过JPA和EclipseLink操作部署在SAP云平台上的HANA数据库实例. 在这篇文章里, ...

  6. Kippo-Failed to load application: 'module' object has no attribute 'IPluggableAuthenticationModules'

    Kippo-Failed to load application: 'module' object has no attribute 'IPluggableAuthenticationModules' ...

  7. android上部署tensorflow

    https://www.jianshu.com/p/ddeb0400452f 按照这个博客就可以 https://github.com/CrystalChen1017/TSFOnAndroid 这个博 ...

  8. python之道09

    整理函数相关知识点,写博客. 看代码写结果 1. def func(): for i in range(3): print(i) return 666 print(func()) # 0 1 2 66 ...

  9. 在Vue将第三方JS库封装为组件使用

    第三方JS库地址:https://github.com/inorganik/CountUp.js 使用NPM进行安装: npm install --save countup 根据官方回答,CountU ...

  10. 01_4_SERVLET声明周期

    01_4_SERVLET声明周期 1. Servlet的生命周期 生命全过程 加载ClassLoader 实例化 new //客户端第一次请求的时候,只new一次 初始化init(ServletCon ...