LeetCode-Combinations
来源:互联网 发布:自学编程语言 知乎 编辑:程序博客网 时间:2024/06/05 06:56
题目: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],]
源码:Java版本
算法分析:深搜,递归。时间复杂度O(n!),空间复杂度O(n)
public class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> results=new ArrayList<List<Integer>>(); Stack<Integer> stack=new Stack<Integer>(); combine(n,0,k,stack,results); return results; } private void combine(int n,int start,int k,Stack<Integer> stack, List<List<Integer>> results) { if(k==0) { results.add((Stack<Integer>)(stack.clone())); return; } for(int i=start;i<n;i++) { stack.push(i+1); combine(n,i+1,k-1,stack,results); stack.pop(); } }}
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
- linux笔记第四部分
- win7 ubuntu
- UILabel自适应大小的新方法boundingRectWithSize:options:attributes:context:
- 关于“Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002”出错
- 如何使用别人的代码 (特指在MFC里面 或者推广为C++里面)
- LeetCode-Combinations
- 【Java学习】混型尝试(1 - 接口)
- NSURLConnection
- 击鼓传花:对比 muduo 与 libevent2 的事件处理效率
- SQL 高级(7) Date 函数
- 关于xmlns:tools使用说明(转)
- HDU 3879 最大树闭合图
- C++遍历目录
- Java中StringBuffer和StringBuilder类总结