当前位置:网站首页>LeetCode 216. Combined Sum III (2022.08.04)
LeetCode 216. Combined Sum III (2022.08.04)
2022-08-05 10:01: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;
}
};
边栏推荐
- PAT乙级-B1020 月饼(25)
- js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)
- 告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
- How to realize the short press and long press detection of the button?
- PAT Class B-B1019 Digital Black Hole (20)
- 19. Server-side session technology Session
- Hundred lines of code launch red hearts, why programmers lose their girlfriends!
- 公众号如何运维?公众号运维专业团队
- Oracle temporary table space role
- 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
猜你喜欢

CCVR eases heterogeneous federated learning based on classifier calibration

偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的

创建一个 Dapp,为什么要选择波卡?

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

Egg framework usage (1)

项目成本控制如何帮助项目成功?

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

阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!

21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)

MySQL内部函数介绍
随机推荐
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
ffmpeg drawtext 添加文本水印
egg框架使用(二)
Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)
Egg framework usage (2)
语音社交软件开发——充分发挥其价值
mysql索引
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
基于MindSpore高效完成图像分割,实现Dice!
5.部署web项目到云服务器
MySQL之数据视图
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
无题十三
MySQL事务
轩辕实验室丨欧盟EVITA项目预研 第一章(四)
蚁剑webshell动态加密连接分析与实践
Keil升级到AC6后,到底有哪些变化?
leetcode: 529. Minesweeper Game
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(下)
seata源码解析:事务状态及全局锁的存储