【LeetCode】77. Combinations
来源:互联网 发布:mac截图存在哪 编辑:程序博客网 时间:2024/06/15 17:48
问题描述
https://leetcode.com/problems/combinations/#/description
Given two integers n and k, return all possible combinations 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],]
算法
类似全排列问题,使用回溯算法即可
代码
public List<List<Integer>> combine(int n, int k) { List<List<Integer>> res = new ArrayList<List<Integer>>(); fun(res, new ArrayList<Integer>(), 0, n, k); return res; } public void fun(List<List<Integer>> res, List<Integer> list, int start, int n, int k) { if(list.size() == k) { res.add(new ArrayList<>(list)); return; } for(int i=start;i<n;i++) { list.add(i+1); fun(res, list, i+1, n, k); list.remove(list.size()-1); } }
阅读全文
0 0
- LeetCode 77. Combinations
- [LeetCode]77.Combinations
- LeetCode --- 77. Combinations
- [Leetcode] 77. Combinations
- [leetcode] 77.Combinations
- leetCode 77.Combinations (组合)
- [leetcode] 77. Combinations
- 77. Combinations LeetCode
- <LeetCode OJ> 77. Combinations
- [LeetCode]77. Combinations
- leetcode 77. Combinations
- LeetCode *** 77. Combinations
- leetcode 77. Combinations
- LeetCode 77. Combinations
- LeetCode-77.Combinations
- LeetCode 77.Combinations
- [LeetCode]problem 77. Combinations
- [LeetCode] 77. Combinations
- maven资源文件的相关配置
- 一篇文章让你彻底搞清楚Python中self的含义
- nginx 配置实战
- LinkedHashMap 实现 LruCache 的底层数据结构?
- Searchable Select 加上远程调用数据功能
- 【LeetCode】77. Combinations
- ubuntu 16.04 下安装PCL
- QT学习中遇到的问题!!!
- [乐意黎]Apache重启后抛order takes one argument, 'allow,deny', 'deny,allow', or 'mutual-failure'
- 排序算法之快速排序
- 在aspx页面中将DataTable中的数据通过GridView导出到Excel中
- java中NIO对文件的读取操作
- Maven学习第二节---入门程序HelloWord
- Elasticsearch聚合