[Leetcode] Combinations 组合数

news/2024/5/19 14:13:19

Combinations

Given two integers n and k, return all possible ombinations of k numbers out of 1 ... n.

For example, If n = 4 and k = 2, a solution is:

[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],
]

回溯法

复杂度

时间 O(N) 空间 O(K)

思路

通过深度优先搜索,回溯出所有可能性即可。

代码

public class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();public List<List<Integer>> combine(int n, int k) {dfs(1, k, n, new ArrayList<Integer>());return res;}private void dfs(int start, int k, int n, List<Integer> tmp){// 当已经选择足够数字时,将tmp加入结果if(k == 0){res.add(new ArrayList<Integer>(tmp));}// 每一种选择数字的可能for(int i = start; i <= n; i++){tmp.add(i);dfs(i + 1, k - 1, n, tmp);tmp.remove(tmp.size() - 1);}}
}

公式法

复杂度

时间 O(N) 空间 O(N)

思路

在数学中,组合数有这么一个性质
$$ C_{n}^{k}=C_{n-1}^{k-1}\cup n+C_{n-1}^{k}$$
所以,我们可以分别求出C(n-1,k-1)和C(n-1,k),并将前者都加上n,最后将两个结果和到一起,就是C(n,k)。而递归的Base条件是当n=0,k=0或者n<k时,返回一个空列表。

注意

当C(n-1,k-1)返回的是空列表时,要加一个空列表进去,否则for循环会被跳过

代码

public class Solution {public List<List<Integer>> combine(int n, int k) {// Recursion: C(n, k) = C(n-1, k-1) U n + C(n-1, k)// Base: C(0, k) C(n, 0) n < k ---> emptyList<List<Integer>> res = new LinkedList<List<Integer>>();if(n < k || n == 0 || k == 0){return res;}// C(n-1, k-1) U nList<List<Integer>> temp = combine(n-1, k-1);List<List<Integer>> part1 = new LinkedList<List<Integer>>();// 加入一个空列表,防止跳过for循环if(temp.isEmpty()){List<Integer> list = new LinkedList<Integer>();temp.add(list);}for(List<Integer> list : temp){list.add(n);part1.add(list);}// C(n-1, k)List<List<Integer>> part2 = combine(n-1, k);res.addAll(part1);res.addAll(part2);return res;}
}

http://lihuaxi.xjx100.cn/news/287015.html

相关文章

java swing中英文支持,java - Swing国际化 - 如何在运行时更新语言 - SO中文参考 - www.soinside.com...

我通过扩展JLabel并覆盖getText来返回对语言选择的评估来解决这个问题。您还需要一些发布/订阅机制来“告诉”您的标签语言已更改。在这里我使用updateLanguage当用户更改语言时&#xff0c;您将触发事件&#xff1a;import com.google.common.eventbus.EventBus;public class …

PyCharm为什么这么牛?

&#xff08;给视学算法加星标&#xff0c;提升Python技能&#xff09;编译&#xff1a;机器之心&#xff0c;作者&#xff1a;Jahongir RahmonovPyCharm 是一种 Python IDE&#xff0c;可以帮助程序员节约时间&#xff0c;提高生产效率。那么具体如何使用呢&#xff1f;本文从…

网络容器

例如&#xff1a;2层的全连接层上加上单独的激活函数层&#xff0c;可以通过Sequential容器封装为一个网络 import tensorflow as tf import numpy as np #导入keras模型&#xff0c;不能使用import keras &#xff0c;它导入的是标准的keras库 from tensorflow import keras …

exkmp解读

题解 P5410 【【模板】扩展 KMP】 posted on 2019-05-20 13:51:22 | under 题解 | 55 一、引言 一个算是冷门的算法&#xff08;在竞赛上&#xff09;&#xff0c;不过其算法思想值得深究。 二、前置知识 kmp 的算法思想&#xff0c;具体可以参考这篇日报。 trie 树&#…

为啥程序员下班后只关显示器从不关电脑?看看各大网站的答案~

本文综合自&#xff1a;csdn原文&#xff1a;https://blog.csdn.net/csdnsevenn/article/details/87887552慕课网原文&#xff1a;https://www.imooc.com/article/30549首百问答的答案&#xff1a;jingmentudou因为你永远不知道什么时间会被叫醒。开个远程就能避免半夜去公司了…

稳压管,TVS管,压敏电阻,气体放电管等电涌保护器器件比较------amoBBS

//稳压管和TVS管特性比较//【相同点】1.都可以限制两端电压在一定得范围内2.长时间耐流值差不多&#xff0c;跟体积功耗有关【不同点】1.电压精度上 稳压管的稳压值比较精确&#xff0c;TVS是在一个范围内2.通流能力上 稳压管的耐涌浪电流很小&#xff0c;而TVS可以达到几百…

java操作跨页的word cell,利用itext 生成pdf,处理cell 跨页问题 [转]

处理方法&#xff1a;PdfPTable table newPdfPTable(1);table.setSplitLate(false);table.setSplitRows(true);开发中的例子&#xff1a;document new Document();Stringseparator System.getProperties().getProperty("file.separator");out new FileOutputStream(C…

模型装配

以Sequential容器封装的一个网络为例&#xff0c;首先创建一个五层的全连接层&#xff0c;用于MNIST手写数字识别&#xff1a; import tensorflow as tf import numpy as np from tensorflow.keras import optimizers,losses,layers,Sequential,datasets#导入优化器&#xff0…