题目

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.

参考答案

 class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = accumulate(nums.begin(),nums.end(),); //首先对数组求和
if( sum & ){ // 如果数组是个奇数,那么不成立,因为例如: 5 = 2+3
return false;
}
int half = sum / ;
std::sort(nums.begin(),nums.end(),std::greater<int>()); // 一定要加,否则time limit exceeded
return foo(nums,half,); // 将 half 作为输入
} bool foo(vector<int>& nums, int half, int index){
for(int i = index ; i <nums.size() ; i++){ // 对于每一个数进行迭代
int h = half - nums[i]; // 对 half 扣除
if(h<) return false;
if(h==) return true;
// 如果上述条件都不满足,说明nums[i] 没办法满足条件,那么需要继续找下一个数,
20        // 即index = i+1。满足即true,不满足就退回到上层,在这一层找下一个数。
if(foo(nums,h,i+) == true) return true;
}
return false;
}
};

LC 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. [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集

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

  5. 416. Partition Equal Subset Sum

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

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

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

  7. 416 Partition Equal Subset Sum 分割相同子集和

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

  8. 【leetcode】416. Partition Equal Subset Sum

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

  9. [刷题] 416 Partition Equal Subset Sum

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

随机推荐

  1. 方阵转置(c++)

    #include #include using namespace std; int main(int argc,char* argv[]) { int a[4][4]={ {0,1,2,3}, {4 ...

  2. jenkins的任务卡住

    今天做jenkins任务的时候,发现一个启动后,一直卡住,在那转圈圈,其实这个时候,任务已经执行完了. 经过分析,因为这个任务是启动一个web服务,直接在机器上执行时,直接占用一个终端. 解决办法,放 ...

  3. 生产环境缺陷来源VS 缺陷管理响应机制

    生产环境缺陷主要来源于用户反馈.版本内遗留.内部反馈和监控后台报警,具体内容包含以下途径: 1.用户反馈: ①  前台电话方式 ②  意见反馈后台 ③  第三方平台:如微博.App Store等渠道 ...

  4. python定时任务实现

    安装 pip install schedule 示例代码 import schedule import time def job(): print("I'm working..." ...

  5. 牛顿法与拟牛顿法(五) L-BFGS 算法

    转自 https://blog.csdn.net/itplus/article/details/21897715

  6. oracle-sql脚本

    select * from dba_users; create tablespace kyc_coo; create user kyc_coo identified by "123456&q ...

  7. Eclipse的下载地址

    下载地址:http://eclipse.org/

  8. Queue class

    #pragma once#include <iostream>#include <iomanip> using namespace std; class Queue{ stru ...

  9. 05-06 Flutter JSON和序列化反序列化、创建模型类转换Json数据、轮播图数据渲染:Flutter创建商品数据模型 、请求Api接口渲染热门商品 推荐商品

    Config.dart class Config{ static String domain='http://jd.itying.com/'; } FocusModel.dart class Focu ...

  10. [Scikit-learn] 1.1 Generalized Linear Models - Bayesian Ridge Regression

    1.1.10. Bayesian Ridge Regression 首先了解一些背景知识:from: https://www.r-bloggers.com/the-bayesian-approach- ...