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.push_back(subans); return ;
        }
        for (int i = start; i <= n; i++)
        {
            subans.push_back(i);
            dfs77(ans, subans, i + 1, n, k);
            subans.pop_back(); // 满足一个条件或者该分支递归完后要删除最后一个
        }
    }
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int > > ans;
        if (n < k || k == 0) return ans;
        vector<int> subans;
        dfs77(ans, subans, 1, n, k);
        return ans;
    }
};
时间: 2024-08-06 15:21:43

leetcode[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]

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 (组合)

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

LeetCode: Letter Combinations of a Phone Number [018]

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

Leetcode:Letter Combinations of a Phone Number 手机键盘字母映射

Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outp

LeetCode: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], ]DFS,递归 1 class Solution { 2 public: 3 vector<vector<int> > an

【一天一道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], ]

&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], ]