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

分析: 组合的实现,之前实现过全排列,递归求解

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        //爆搜
        vector<vector<int>> res;
        if (k==0 || n==0)
            return res;

        for(int base=1; base<=(n-k+1); base++){
            vector<int> tempres(1,base);
            helper(base,n,k-1,tempres,res);
        }
        return res;
    }
    void helper(int base,int n, int num, vector<int>& tempres, vector<vector<int>>& res){
        if(num==0){
            res.push_back(tempres);
            return;
        }
        if(base+num-1>n){
            return;
        }
        for(int i=base+1; i<=n; i++){
            tempres.push_back(i);
            helper(i,n,num-1,tempres,res);
            tempres.pop_back();
        }
    }
};
时间: 2024-12-14 01:39:37

Combinations的相关文章

17. 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" Output: ["ad", "ae", &q

Letter Combinations of a Phone Number

1. Question 给一个数字字符串,按照电话上各个数字对应的字母,返回该字符串代表的所有可能的字母组合. 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 stri

LeetCode77: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 给定一个数n,求1到n之间的所有的k个数的组合. 这个题目可以在纸上画下草图,明显可以用递

【leetcode】Combinations (middle)

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], ] 思路:有点像0-1背包问题, 对于从1-n的每一个数字都可以选择放入答案 和不放入答案. 当长度达到k时就是一个符合条件的解. 递归的

LeetCode--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" Output: ["ad", "ae", &q

【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]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], ] 基本思想: 回溯法 代码: vector<int> record; //C++ vector<vector<

leetcode第18题--Letter Combinations of a Phone Number

Problem: 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 [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"

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