[C++]LeetCode: 83 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],
]

思路:回溯法

构造一个辅助函数,combine_helper.

(1)递归一次,填入一个数字

(2)填入的数字,不能是小于当前数字的值,防止重复

(3)回溯:pop_back()最后加上的一个数字,回溯到上一层

(4)结束条件,当填入的数字达到k个数字时,当前填写完毕,回溯。

关于回溯的具体实现过程,这篇文章有个动画,解释的很生动: Backtracking

Attention:

1. 注意我们填入数字必须是递增的,否则会导致重复数字。

2. 掌握回溯的思想精髓。pop_back很关键。回溯到上一层。

复杂度:非确定性多项式时间复杂性类, NP问题

AC Code:

class Solution {
public:
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int> > ret;
        if(n == 0 || k == 0 || n < k) return ret;
        vector<int> tmp;
        combine_helper(0, 0, n, k, tmp, ret);
        return ret;
    }

private:
    void combine_helper(int start, int num, int n, int k, vector<int> tmp, vector<vector<int> >& ret)
    {
        if(num == k)
        {
            ret.push_back(tmp);
            return;
        }

        for(int i = start; i < n; i++)
        {
            tmp.push_back(i+1);
            combine_helper(i+1, num+1, n, k, tmp, ret);
            tmp.pop_back();
        }
    }
};
时间: 2024-08-06 22:37:47

[C++]LeetCode: 83 Combinations (回溯法)的相关文章

从Leetcode的Combination Sum系列谈起回溯法

在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2.3都可以通过回溯法来解决,其实4也可以,不过由于递归地比较深,采用回溯法会出现TLE.因此本文只讨论前三题. 什么是回溯法?回溯法是一种选优搜索法,按选优条件向前搜索以达到目标.当探索到某一步时,发现原先的选择并不优或达不到目标,就退回异步重新选择.回溯法是深度优先搜索的一种,但回溯法在求解过程不

[Leetcode] Backtracking回溯法解题思路

碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大.感觉如果要顺利的把题刷下去,必须先要把做的几道题题总结一下. 先放上参考的web: https://segmentfault.com/a/1190000006121957 http://summerisgreen.com/blog/2017-07-07-2017-07-07-算法技巧-backtr

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】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

[leetcode]79.Search Word 回溯法

/** * Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not

[LeetCode] 679. 24 Game(回溯法)

传送门 Description You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, )to get the value of 24. Example 1: Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24 Example 2

Leetcode之回溯法专题-39. 组合总数(Combination Sum)

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选取. 说明: 所有数字(包括 target)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ] 示例 2: 输入: candidates = [2,3,5], target

[LeetCode] Factor Combinations 因子组合

Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a function that takes an integer n and return all possible combinations of its factors. Note: Each combination's factors must be sorted ascending, for examp

经典算法学习之回溯法

回溯法的应用范围:只要能把待求解的问题分成不太多的步骤,每个步骤又只有不太多的选择就可以考虑使用回溯法. 若用回溯法求问题的所有解时,要回溯到根,且根结点的所有可行的子树都要已被搜索遍才结束. 而若使用回溯法求任一个解时,只要搜索到问题的一个解就可以结束. 回溯法将问题的候选解按照某一顺序逐一枚举和检验.当发现当前候选解不可能是解时,就选择下一个候选解,若当前候选解符合要求,且还未达到求解的规模,则继续扩展当前候 选解的规模,如果候选解已经满足了所有要求,并且也达到了问题的规模,那么该候选解就是