当前位置:网站首页>力扣(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;
}
};
边栏推荐
- Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
- leetcode: 529. Minesweeper Game
- 入门 Polkadot 平行链开发,看这一篇就够了
- PAT Grade B-B1020 Mooncake(25)
- CCVR eases heterogeneous federated learning based on classifier calibration
- 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
- Egg framework usage (1)
- Why do I recommend using smart async?
- Seata source code analysis: initialization process of TM RM client
- Jenkins使用手册(2) —— 软件配置
猜你喜欢

高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer

Example of Noise Calculation for Amplifier OPA855

After Keil upgrades to AC6, what changes?

IDEA performs the Test operation, resulting in duplicate data when data is inserted

皕杰报表的下拉框联动

dotnet OpenXML 解析 PPT 图表 面积图入门

First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad

哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?

2.4G无线收发模块的应用

如何实现按键的短按、长按检测?
随机推荐
无题十四
正则表达式replaceAll()方法具有什么功能呢?
Tanabata romantic date without overtime, RPA robot helps you get the job done
shell脚本实例
JS逆向入门学习之回收商网,手机号码简易加密解析
无题一
2022.8.3
What is CRM Decision Analysis Management?
Excuse me, guys, is it impossible to synchronize two databases in real time using Flink SQL CDC?
openpyxl to manipulate Excel files
Egg framework usage (1)
手把手教你纯c实现异常捕获try-catch组件
无题十二
5.部署web项目到云服务器
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
2022/8/4 考试总结
Jenkins使用手册(2) —— 软件配置
蚁剑webshell动态加密连接分析与实践
QSS 选择器
皕杰报表的下拉框联动