[leet code] Combinations
来源:互联网 发布:航空医学知乎 编辑:程序博客网 时间:2024/06/06 02:03
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],]
=================
Note that in each combination, former number is smaller than the following number. In another word, [1, 2] and [2, 1] is considered as the same combination, and only [1, 2] is required as the result in this problem.
As usual, if we don't have any idea about the solution, we can try to manually process 1-2 examples. Here is an example I tried, n=5, k=3:
From this example, we can see that in the first position of the resulting combinations we can chose number 1-5. Assume that we chose 1 for the 1 position of the combination, then we can choose 2-5 for the second position. Till we chosen numbers for all position, we can have one possible combination.
However, when we chose 3 for the first position and 5 for the second position, we don't have any other number to choose for the 3rd position. (former number must be smaller than the following number).
Now we have the rules of this problem, and we can try to implement these rules. For choosing number in a specific layer, we can use a iteration, e.g. in the first layer, we try 1-5. While for step in another layer, we can use recursive call. Parameters we need for a recursive call are the current combination, the combination result set, the n, the k, and the start point of iteration (e.g. if we chose 1 for the first position, start point of 2nd position is 2; while if we chose 2 for the first position, start point of second position is 3).
public class Solution { public ArrayList<ArrayList<Integer>> combine(int n, int k) { ArrayList<ArrayList<Integer>> combSet = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> comb = new ArrayList<Integer>(); if(n<k) return combSet; helper(n, k, combSet, comb, 1); return combSet; } public void helper(int n, int k, ArrayList<ArrayList<Integer>> combSet, ArrayList<Integer> comb, int start){ // one possible combinition constructured if(comb.size() == k){ combSet.add(new ArrayList<Integer> (comb)); return; } else{ for(int i=start; i<=n; i++){// try each possibility number in current position comb.add(i); helper(n, k, combSet, comb, i+1); // after selecting number for current position, process next position comb.remove(comb.size()-1); // clear the current position to try next possible number } } }}
- [leet code] Combinations
- [leet code] Letter Combinations of a Phone Number
- Leet Code 17 Letter Combinations of a Phone Number
- Leet Code OJ 17. Letter Combinations of a Phone Number
- [leet code] Gray Code
- code Combinations
- [leet code] Same Tree
- [leet code] Reverse Integer
- [leet code] Remove Element
- [leet code] Maximum Subarray
- [leet code] Climbing Stairs
- [leet code] Symmetric Tree
- [leet code] Permutations
- [leet code] Generate Parentheses
- [leet code] Sort Colors
- [leet code] Unique Paths
- [leet code] Rotate Image
- [leet code] Plus One
- 树莓派配置无线连接与无线AP热点
- hdu 3277 二分+拆点最大流
- iOS 如何进行逆向工程?
- 【数学基础】快速幂
- ios系列教程之用instruments来检验你的app
- [leet code] Combinations
- Sort Colors
- CGRectInset、CGRectOffset、等对比整理
- 最长公共子序列
- CMTimeMake和CMTimeMakeWithSeconds 详解
- Java "Hello World"
- 码农面试经验贴汇总
- Binary Tree Level Order Traversal II
- InputStream与String,Byte之间互转