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

思路

典型的递归回溯法。
(1)递归一次,填入一个数字
(2) 填入的数字,不能是小于当前数字的值,防止重复
(3 )回溯:记得pop_back()最后加上的一个数字,回溯到上一层。
(4) 结束条件:填写够了k个数字的时候,当前填写完毕,回溯

代码

    /**------------------------------------
    *   日期:2015-02-06
    *   作者:SJF0115
    *   题目: 77.Combinations
    *   网址:https://oj.leetcode.com/problems/combinations/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        vector<vector<int> > combine(int n, int k) {
            vector<vector<int> > result;
            vector<int> path;
            DFS(n,k,result,path,1);
            return result;
        }
    private:
        void DFS(int n,int k,vector<vector<int> > &result,vector<int> &path,int index){
            if(path.size() == k){
                result.push_back(path);
                return;
            }//if
            for(int i = index;i <= n;++i){
                path.push_back(i);
                DFS(n,k,result,path,i+1);
                path.pop_back();
            }//for
        }
    };

    int main(){
        Solution s;
        int n = 5;
        int k = 2;
        vector<vector<int> > result = s.combine(n,k);
        // 输出
        for(int i = 0;i < result.size();++i){
            for(int j = 0;j < k;++j){
                cout<<result[i][j]<<" ";
            }//for
            cout<<endl;
        }//for
        return 0;
    }

运行时间

时间: 2025-01-26 11:28:59

[LeetCode]77.Combinations的相关文章

[LeetCode]78.Subsets

题目 Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3]

[经典面试题]排列组合专题

[LeetCode]31.NextPermutation [LeetCode]46.Permutations [LeetCode]47.Permutations II STL系列之十 全排列(百度迅雷笔试题) [LeetCode]77.Combinations [LeetCode]39.Combination Sum [LeetCode]40.Combination Sum II [LeetCode]78.Subsets 资料: 算法之排列与组合算法 全排列生成算法 求二维数组的全排列组合,二位

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode]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"

LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译 给定一个数字字符串,返回所有这些数字可以表示的字母组合. 一个数字到字母的映射(就像电话按钮)如下图所示. 输入:数字字符串"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 备注:尽管以上答案是无序的,如果你想的话你的答案可以是

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

[LeetCode]40.Combination Sum II

[题目] 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 target) will be

leetcode 22 Generate Parentheses关于C++ string的问题

问题描述 leetcode 22 Generate Parentheses关于C++ string的问题 /* 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: "((()))", "(()())", "(())