当前位置:网站首页>Leetcode77. 组合
Leetcode77. 组合
2022-08-06 08:18:00 【Java全栈研发大联盟】

题目传送地址: https://leetcode.cn/problems/combinations/
运行效率
解题思路
从1到n个数里面选k个数, 我打个比方吧,从1到6里面选3个数。 那基本上最后的结果序列都是以3或4或5或6来结尾
class Solution {
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
//打个比方吧,从1到6里面选3个数。 那基本上最后的结果序列都是以3或4或5或6来结尾
for (int i = k; i <= n; i++) {
List<List<Integer>> lists = combineRecur(i, k);
res.addAll(lists);
}
return res;
}
/** * 返回所有以m结尾的且含有k个元素的序列 * * @param m * @param k * @return */
public static List<List<Integer>> combineRecur(int m, int k) {
//处理边界情况
List<List<Integer>> result = new ArrayList<>();
if (m == k) {
List<Integer> list = new ArrayList<>();
int i = 1;
while (i <= m) {
list.add(i);
i++;
}
result.add(list);
return result;
}
if (k == 1) {
List<Integer> list = new ArrayList<>();
list.add(m);
result.add(list);
return result;
}
if (k == 2) {
//m已经是序列中的最后一个元素了,那还有一个元素就得从1到m-1中任意选一个
int i = 1;
while (i <= m-1) {
List<Integer> list = new ArrayList<>();
list.add(i);
list.add(m);
result.add(list);
i++;
}
return result;
}
for (int i = k-1; i < m; i++) {
List<List<Integer>> combine = combineRecur(i, k - 1);
for (List<Integer> list : combine) {
List<Integer> list1 = new ArrayList<>(list);
list1.add(m);
result.add(list1);
}
}
return result;
}
}
边栏推荐
- DemographicTable 新的基线特征表绘制 R包
- Ceph性能测试方案收集列表
- 剑指 Offer 56 - I. 数组中数字出现的次数
- QianBase 运维实用命令
- I set the global mapping table prefix in yml, but the database does not recognize it
- 20220803模拟
- Test case design method - detailed explanation of scenario method
- 5. 自动引入打包资源 plugins的使用
- Tencent Cloud VOD uploads video files to solve the path problem
- ggplot2绘图进阶:如何在不同分面添加不同图形
猜你喜欢

Day020 Method Overriding and Polymorphism

CobaltStrike图片远程上线(实验篇)

不会吧,不会吧都2022年了你不会还不知道Jmeter原理吧

MySQL数据库的数据类型和基于MySQL数据类型的综合实例项目

代码签名证书可以解决软件被杀毒软件报毒提醒吗?

How can machinery manufacturing companies use ERP systems to manage production schedules?

深度学习——怎样读取本地数据作为训练集和测试集

代码签名证书多少钱?

七夕玫瑰花合集

LeetCode——345. 反转字符串中的元音字母
随机推荐
LeetCode - 1047. Remove all adjacent duplicates in a string
40多条令人爆笑的注释
C语言力扣第59题螺旋矩阵②。模拟矩阵
【云原生--Kubernetes】配置管理
限制命令长度如何反弹shell
中国电子学会青少年等级考试五级原题
Why do interviewers keep asking technical questions on your resume until they can't answer them?
[科普文] 搞 Web3 要学习哪些基础知识?
CPU Architecture at a Glance
JMeter代理录制手机app
R语言:决策树结果可视化
EsgynDB Troubleshooting - 网卡MTU导致跨网段访问数据库失败
【Yugong Series】August 2022 Go Teaching Course 030-Object Inheritance
2022-08-05:以下go语言代码输出什么?A:65, string;B:A, string;C:65, int;D:报错。
不会吧,不会吧都2022年了你不会还不知道Jmeter原理吧
自动化测试定位不到元素?可能是 frame 在搞鬼
BuuWeb
How to ensure the security of NFT from the suspected abolition of the magic core
CSDN official plug-in
selenium4.0以上元素被定位