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

 [1,2],

 [1,3],

 [1,4],

]

(二)解题

本题大意:给定两个整形数,n和k,从1~n中找出所有k个数的组合。

这题是典型的回溯问题。分析一下当n=4,k=3的情况:

维持一个vector变量,当它的长度小于3时,按顺序往里面放数字。

第一个满足长度等于3的组合为1,2,3,接下来回溯弹出3,继续放数字4

第二个满足长度等于3的组合为1,2,4,然后回溯弹出4,没有数字放了,就继续回溯弹出2,再放入3和4

第三个满足长度等于3的组合为1,3,4,然后回溯弹出3,4和1,放入2,3,4

第四个满足长度等于3的组合为2,3,4。至此,算法结束。

class Solution {
public:
    vector<vector<int>> ret;
    vector<vector<int>> combine(int n, int k) {
        vector<int> temp;
        dfscombine(n,1,k,temp);
        return ret;
    }
    void dfscombine(int n,int start,int k,vector<int> & temp)
    {
        if(temp.size()==k){//当长度为k时,将结果存在ret中
            ret.push_back(temp);
            return;
        }
        for(int i = start ; i <= n ; i++)
        {
            temp.push_back(i);//放入数字
            dfscombine(n,i+1,k,temp);
            temp.pop_back();//回溯到上一步
        }
    }
};
时间: 2025-01-04 00:53:32

【一天一道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[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】#22. Generate Parentheses

一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "(

【一天一道LeetCode】#93. Restore IP Addresses

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return [&qu

【一天一道LeetCode】#40. Combination Sum II

一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including

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】#219. Contains Duplicate II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]and the differen