当前位置:网站首页>力扣(LeetCode)216. 组合总和 III(2022.08.04)
力扣(LeetCode)216. 组合总和 III(2022.08.04)
2022-08-05 09:55:00 【ChaoYue_miku】
找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:
只使用数字1到9
每个数字 最多使用一次
返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。
示例 1:
输入: k = 3, n = 7
输出: [[1,2,4]]
解释:
1 + 2 + 4 = 7
没有其他符合的组合了。
示例 2:
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解释:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
没有其他符合的组合了。
示例 3:
输入: k = 4, n = 1
输出: []
解释: 不存在有效的组合。
在[1,9]范围内使用4个不同的数字,我们可以得到的最小和是1+2+3+4 = 10,因为10 > 1,没有有效的组合。
提示:
2 <= k <= 9
1 <= n <= 60
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/combination-sum-iii
方法一:二进制枚举
C++提交内容:
class Solution {
public:
vector<int> temp;
vector<vector<int>> ans;
bool check(int mask, int k, int n) {
temp.clear();
for (int i = 0; i < 9; ++i) {
if ((1 << i) & mask) {
temp.push_back(i + 1);
}
}
return temp.size() == k && accumulate(temp.begin(), temp.end(), 0) == n;
}
vector<vector<int>> combinationSum3(int k, int n) {
for (int mask = 0; mask < (1 << 9); ++mask) {
if (check(mask, k, n)) {
ans.emplace_back(temp);
}
}
return ans;
}
};
边栏推荐
- 告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
- 浅析WSGI协议
- PAT乙级-B1019 数字黑洞(20)
- QSS 选择器
- Development common manual link sharing
- Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
- Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
- Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
- 无题六
- 偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
猜你喜欢

After Keil upgrades to AC6, what changes?

egg框架使用(一)

首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析

ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...

mysql进阶(二十七)数据库索引原理

seata源码解析:事务状态及全局锁的存储

告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来

NowCoderTOP35-40——持续更新ing

Jenkins使用手册(2) —— 软件配置

Keil升级到AC6后,到底有哪些变化?
随机推荐
无题十四
新白娘子传奇系列
Imitation SBUS fixed with serial data conversion
What is the function of the regular expression replaceFirst() method?
IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
Overall design and implementation of Kubernetes-based microservice project
openpyxl to manipulate Excel files
手写柯里化 - toString 理解
无题五
Open Source Summer | How OpenHarmony Query Device Type (eTS)
What is the function of the regular expression replaceAll() method?
为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
After Keil upgrades to AC6, what changes?
长达四年的减肥记录
[Unity] [UGUI] [Display text on the screen]
手把手教你纯c实现异常捕获try-catch组件
入门 Polkadot 平行链开发,看这一篇就够了
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
tensorflow.keras cannot introduce layers
The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf