当前位置:网站首页>Leetode 416.分割等和子集
Leetode 416.分割等和子集
2022-07-21 14:22:00 【进击的code儿】
416.分割等和子集
一、题目
原题链接:416.分割等和子集
1.题目描述
给你一个 只包含正整数 的 非空 数组 nums
。请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。
示例 1:
输入:nums = [1,5,11,5]
输出:true
解释:数组可以分割成 [1, 5, 5] 和 [11] 。
示例 2:
输入:nums = [1,2,3,5]
输出:false
解释:数组不能分割成两个元素和相等的子集。
提示:
1 <= nums.length <= 200
1 <= nums[i] <= 100
2.基础框架
C++基础框架代码如下:
bool canPartition(vector<int>& nums) {
}
3.解题思路
- 题目分析
- 题目目标是判断数组是否可以分为两个子集,可以为true,否则为false。
- 这两个子集中的元素可以是不连续的,所以不能采用前缀和的方式解题。
- 得想尽办法证明它是有两个子集的的元素和相等。
- 首先将nums数组的元素和求出来,为
sum
。只要证明集合中存在元素和为sum / 2
的组合,则表明nums数组可以被分割成两个元素和相等的子集。 - 如果
sum % 2 == 1
,说明不存在两个元素和相同的子集,返回false。 - 每个元素只能取一次。
- 到这里,就会发现,跟01背包的问题解法一样,每个元素值相当于是01背包中的物品,且只能取一次,01背包中的容量也就是target, 在这里就是
sum / 2
,将这两要素代入01背包公式解决就好了。
实现代码:
bool canPartition(vector<int>& nums) { int sum = 0; for (int i = 0; i < nums.size(); i++) sum += nums[i]; if (sum % 2 == 1) return false; int target = sum / 2; // 相当于01背包中的容量 memset(dp, 0, sizeof(dp)); for (int i = 0; i < nums.size(); i++) for (int j = target; j >= nums[i]; j--) dp[j] = max(dp[j], dp[ j - nums[i] ] + nums[i]); if (target == dp[target]) //该情况表明集合中刚好元素和为target return true; return false; }
边栏推荐
- The state Internet Information Office made a decision on the administrative punishment related to the network security review of didi Global Co., Ltd. in accordance with the law
- 2022长三角工业自动化展会将于10月在南京国际展览中心召开
- 国产统信UOS系统运行小程序的探索
- 一些工具改造
- 文件操作管理
- bypass 某狗sql和xss
- 今天面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
- 小道仙博客【开源个人博客】
- Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场
- The question is: can you successfully apply for 8g memory on a machine with 4GB physical memory?
猜你喜欢
Discussion on passing the d-shield PHP webshell without killing horses
Discussion on killing free exe Technology
Idea connects to MySQL database
haproxy2.6负载安装配置
Niuke online question brushing - day 3
Ravendb fully transactional NoSQL document database
浅析二层工业交换机的特点
如何高效地实现基础设备可视化
前两天面了个腾讯拿 38K 出来的,让我见识到了基础的天花板,今天share给大家~
Revit API:EditScope
随机推荐
Discussion on ASP webshell+ ice scorpion free horse killing
6.1 恶意代码防范
【读书会第13期】+第三章 视频文件的编码格式
C语言的文件操作
Encryption and decryption of yii2
NETCORE - Middleware (1)
Openai officially announced that dall-e will open its beta to 1million users
D3.js + canvas drawing organization chart
Tencent took out 38K two days ago, which showed me the basic ceiling. Today share is for you~
数据中心线缆管理
Control of semiconductor refrigeration and heating based on general single chip microcomputer control of cold and hot head in mobile phone radiator massage instrument
The state Internet Information Office made a decision on the administrative punishment related to the network security review of didi Global Co., Ltd. in accordance with the law
今天面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
如何高效地实现基础设备可视化
[wechat applet authorization] obtain the user's mobile number and nickname
Talk about 36 tips of interface design
D3.js + Canvas 绘制组织结构图
金鱼哥RHCA回忆录:CL210集成身份管理--项目组织管理+章节实验
Ravendb fully transactional NoSQL document database
国家互联网信息办公室有关负责人就对滴滴全球股份有限公司依法作出网络安全审查相关行政处罚的决定答记者问