[leedcode 77] 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 {
    List<List<Integer>> res;
    List<Integer> seq;
    public List<List<Integer>> combine(int n, int k) {
        //注意分析题意,可选数组时从1到n,因此起始findCom函数的start参数需要从1开始
        res=new ArrayList<List<Integer>>();
        seq=new ArrayList<Integer>();
        findCom(n,1,0,k);
        return res;
    }
    public void findCom(int n,int start,int level,int k){
        if(level==k){
           res.add(new ArrayList<Integer>(seq));
           return;
        }
        for(int i=start;i<=n;i++){//注意i可以为n
            seq.add(i);
            findCom(n,i+1,level+1,k);
            seq.remove(seq.size()-1);
        }
    }
}
时间: 2024-12-27 07:55:29

[leedcode 77] Combinations的相关文章

[Lintcode]152. Combinations/[Leetcode]77. Combinations

152. Combinations/77. Combinations 本题难度: Medium Topic: Search & Recursion Description Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example Given n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2]

77. 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], ] Hide Tags Backtracking 链接:  http://leetcode.com/problems/combin

leetcode 77 Combinations ----- java

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], ] 求C(k,n)的所有结果. 利用回溯(backtracking)来求解释道题是比较简单且效率较高的. public class Sol

【一天一道LeetCode】#77. Combinations

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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]

&lt;LeetCode OJ&gt; 77. Combinations

Total Accepted: 69360 Total Submissions: 206274 Difficulty: Medium 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], ]

leetCode 77.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], ] 思路:此题意思是给一个n,和k,求1-n的k个数的组合.没有反复(位置交换算反复).可用排列组合的统一公式求解. 代码例如以下: p

&amp;lt;LeetCode OJ&amp;gt; 77. Combinations

Total Accepted: 69360 Total Submissions: 206274 Difficulty: Medium 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], ]

77. Combinations(回溯)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: Input: n = 4, k = 2 Output: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 1 class Solution { 2 List<List<Integer>> res = new ArrayList<>()

leetcode[77] Combinations

给定n和k,从1到n中选k个数,存到结果中.其实就是组合问题.例如 If n = 3, k = 2, 结果是 { 1,2], [1,3], [2,3] }; 思路:利用回溯法. class Solution { public: void dfs77(vector<vector<int > > &ans, vector<int> subans, int start, int n, int k) { if (subans.size() == k) { ans.pus