Leetcode ——Partition Equal Subset Sum
Question
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.
Solution
0-1背包问题。 dp[j] = dp[j] || dp [j - n];
Code
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = 0;
for (int n : nums)
sum += n;
if (sum % 2 != 0)
return false;
sum /= 2;
return subsetsum(nums, sum);
}
bool subsetsum(vector<int>& nums, int sum) {
int dp[sum + 1] = {false};
dp[0] = true;
for (int i = 0; i < nums.size(); i++) {
for (int j = sum; j >= nums[i]; j--) {
dp[j] = dp[j] || dp[j - nums[i]];
}
}
return dp[sum];
}
};
Leetcode ——Partition Equal Subset Sum的更多相关文章
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode: Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- LeetCode—— Partition Equal Subset Sum
Question Given a non-empty array containing only positive integers, find if the array can be partiti ...
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- 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 ...
- 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 ...
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
随机推荐
- scrapy的post登录:renren
# -*- coding: utf-8 -*- import scrapy class RenrenSpider(scrapy.Spider): name = 'renren' allowed_dom ...
- C++ 指向数组的指针
如果您对 C++ 指针的概念有所了解,那么就可以开始本章的学习.数组名是一个指向数组中第一个元素的常量指针.因此,在下面的声明中: double balance[50]; balance 是一个指向 ...
- 虚拟机VMware的网络设置出了问题会导致很多莫名的错误
邪门地CentOS内软件安装失败问题:Xshell与虚拟机的各种连接失败:CentOS下eth0没显示ip地址. 这些原因竟然是一个,虚拟机VMware的网络设置出了问题. 恢复初始设置即可.
- U盘安装win10操作系统
https://www.zhihu.com/question/39207359 1:进入微软官方网站,点击立即下载工具,下载完成mediacreationtool,双击打开,接受协议 https ...
- Python自动发邮件-yagmail库
之前写过用标准库使用Python Smtplib和email发送邮件,感觉很繁琐,久了不用之后便忘记了.前几天看知乎哪些Python库让你相见恨晚?,看到了yagmail第三方库,学习过程中遇到一些问 ...
- hdu1542 Atlantis(矩阵面积的并)
这个题算是我的第一个扫描线的题,扫描线算是一种思想吧,用到线段树+离散化.感觉高大上. 主要参考了这位大神的博客. http://www.cnblogs.com/kuangbin/archive/20 ...
- sql 查询不存在左表的数据
select * from zyz_mgr_wlcyiduifu a left join WLCInformation b ona.wlcId=b.WLCInvestorApplyID where b ...
- cordova+Android Studio 1.0+ionic+win7(转)
转自http://blog.csdn.net/fuyunww/article/details/42216125 目录(?)[-] 在项目目录下执行 a创建工程 b添加平台支持 c添加插件在Androi ...
- VS2010/MFC编程入门之四十九(图形图像:CDC类及其屏幕绘图函数)
上一节中鸡啄米讲了文本输出的知识,本节的主要内容是CDC类及其屏幕绘图函数. CDC类简介 CDC类是一个设备上下文类. CDC类提供了用来处理显示器或打印机等设备上下文的成员函数,还有处理与窗口客户 ...
- 20154312 曾林 EXP6 信息搜集与漏洞扫描
目录 1.实验后回答问题 2.实验总结与体会 3.实践过程记录 --3.1.信息收集 ----3.1.1.whois查询 ----3.1.2.nslookup,dig查询 ----3.1.3.trac ...