leetcode || 77、Combinations

problem:

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

题意:输出1~n的 k 个数字的所有排列组合

thinking:

(1)看到题就应想到用DFS深搜法

(2)深搜的难点在于下一步怎么处理,这里开一个K大小的数组,记录深搜的每一步获取的数字

(3)时间复杂度为O(K*N),空间复杂度为O(k)

code:

class Solution {
private:
    vector<vector<int> > ret;
    vector<int> tmp;
public:
    vector<vector<int> > combine(int n, int k) {
        ret.clear();
        tmp.resize(k);
        dfs(1,n,k,1);
       return ret;

    }
protected:
    void dfs(int dep, int n, int k,int start)
    {
       if(dep>k)
       {
           ret.push_back(tmp);
           return;
       }
       for(int i=start;i<=n;i++)
       {
           tmp[dep-1]=i;
           dfs(dep+1,n,k,i+1);
       }
    }
};
时间: 2024-09-29 18:30:53

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 || 118、Pascal&#39;s Triangle

problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

leetcode || 119、Pascal&#39;s Triangle II

problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

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

LeetCode 77. 组合(Combinations)

题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 解题思路 回溯法,每次遍历到一个元素分为放入与不放入集合两种情况,若集合长度为k,则加入到结果中. 代码 1 class Solution { 2 public: 3 vector<vector<int>> combine(int n, int k) {

【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" Outpu

[LeetCode][JavaScript]Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 40709 Total Submissions: 157759My Submissions Question Solution Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just l