题目

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. SNMP 协议介绍 转载

    一.SNMP简单概述 1.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议& ...

  2. 解决python在命令行中运行时导入包失败,出现错误信息 "ModuleNotFoundError: No module named ***"

    转自https://www.cnblogs.com/dreamyu/p/7889959.html https://www.cnblogs.com/lifeofershisui/p/8135702.ht ...

  3. New in Python 3.8.0

    Python 3.8.0 发布时间: Oct. 14, 2019 这是一个Python3.8.0的稳定发行版. Python3.8.0是最新的Python编程语言发行版,ta包含了许多新的特征和优化. ...

  4. Visual Studio Team Systems

    https://www.cnblogs.com/33568639/archive/2008/12/29/1364222.html https://baike.sogou.com/v7818386.ht ...

  5. SQL-W3School-高级:SQL BETWEEN 操作符

    ylbtech-SQL-W3School-高级:SQL BETWEEN 操作符 1.返回顶部 1. BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. BETWEE ...

  6. StateListDrawable

    可供设置的属性如下: drawable:引用的Drawable位图,我们可以把他放到最前面,就表示组件的正常状态~ state_focused:是否获得焦点 state_window_focused: ...

  7. Python xlwt 模块执行出错Exception: String longer than 32767 characters

    使用Python搜集数据时用到xlwt保存到excel文件,但是数据量有点大时出现 Exception: String longer than 32767 characters 搜索类似的问题都是建议 ...

  8. PAT 甲级 1012 The Best Rank (25 分)(结构体排序)

    题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同时,我们鼓励学生强调自己的最优秀队伍 - 也就是说 ...

  9. 用Excel如何将文本转换为数字的七种方法

    用Excel如何将文本转换为数字的七种方法 当下,很多工作都会用到Excel,下面本文分步介绍了如何将包含文本的Excel单元格转换为包含数字的单元格. 概述: 当导入在另一程序(如 dBASE 或  ...

  10. TextView的封装和自定义

    实现的效果如下: #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property (nonatomic , ...