leetcode Combinations
来源:互联网 发布:java聊天室源码详解 编辑:程序博客网 时间:2024/05/22 01:15
Combinations 原题地址:
https://oj.leetcode.com/problems/combinations/
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 class Solution { private List<List<Integer>> list = new ArrayList<List<Integer>>();public List<List<Integer>> combine(int n, int k) {if (k > n || k == 0)return list;LinkedList<Integer> _list = new LinkedList<Integer>();helpCombine(_list, n, 1, k);return list;}private void helpCombine(LinkedList<Integer> _list, int n, int idx, int k) {if (k == 0) {LinkedList<Integer> temp = (LinkedList<Integer>) _list.clone();list.add(temp);return;}if (idx > n)return;if (idx + k == n+1) {LinkedList<Integer> temp = (LinkedList<Integer>) _list.clone();for (int i = idx; i <= n; i++) {temp.add(i);}list.add(temp);return;}_list.add(idx);helpCombine(_list, n, idx+1, k-1);_list.pollLast();helpCombine(_list, n, idx+1, k);}}
0 0
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [LeetCode] Combinations
- Leetcode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [Leetcode] Combinations
- [leetcode] Combinations
- [LeetCode]Combinations
- [leetcode]Combinations
- LeetCode-Combinations
- [leetcode] Combinations
- LeetCode - Combinations
- 【Leetcode】Combinations
- Leetcode: Combinations
- 【leetcode】Combinations
- 调用域名注册api,查询所有域名组合脚本
- AVL树 Java实现
- 第12周项目1 阅读程序(静态变量、递归函数、默认参数、函数模板等)
- Unity碰撞盒光源声效及树木制作
- Hibernate框架(二)POJO对象的操作
- leetcode Combinations
- MySQL常用函数
- 静态库和动态库
- 利用C语言实现在客户端和服务器端发送一个整数
- Network Connections UVA 793(并查集)
- 第11周项目2.2 四个数的最大公约数
- UVa 10976 - Fractions Again?!
- sqlplus 列设置与报表应用
- Hibernate框架(三)框架中的关系映射