【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时就是一个符合条件的解。

递归的代码,AC了。只要注意状态的还原就好。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

class Solution {
public:
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int>> ans;
        combinepart(ans, 1, k, n);
        return ans;
    }

    void combinepart(vector<vector<int>> &ans, int num, int k, int n)
    {
        static int i = 0;
        static vector<int> partans;
        if(num - 1 > n || partans.size() + (n - num + 2) < k) return; //数字超过了n 或者即使后面数字全部压入长度也不够的时候 直接返回 避免不必要的计算
        if(i == k)
        {
            ans.push_back(partans);
            return;
        }

        partans.push_back(num); //放入num
        i++;
        combinepart(ans, num + 1, k, n);

        partans.pop_back();
        i--;
        combinepart(ans, num + 1, k, n);//不放入num
    }
};

int main()
{
    Solution s;
    vector<vector<int>> ans = s.combine(3,3);

    return 0;
}

网上有非递归的代码,可是我好困,懒得看... 速度都差不多的,因为我的递归截枝了,没有多余的操作。

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

    for(int i=1;i<=n-k+1;i++){
        ret.push_back(vector<int>(1,i));
    }

    for(int i=2;i<=k;i++){
        int num=ret.size();
        for(int j=0;j<num;j++){
            int last=ret[j].back();
            vector<int> pretmp=ret[j];
            ret[j].push_back(last+1);
            for(int p=last+2;p+k-i<=n;p++){
                vector<int> tmp=pretmp;
                tmp.push_back(p);
                ret.push_back(tmp);
            }
        }
    }

    return ret;
}
};
时间: 2024-10-17 12:17:52

【leetcode】Combinations (middle)的相关文章

【LeetCode】Combinations 解题报告

好像自从上次 CSDN Blog 系统故障后,博文阅读量过百后系统不再发放C币,尽管我也不知道C币有啥用. [题目] 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], ] [解析] 题意

【LeetCode】Combinations

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], ] 递推点:加入i后,下一个加入的元素需要遍历i+1~n 因此可以基于k做递归. base case: k=1,

【Leetcode】Combinations (Backtracking)

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], ] 这是一道经典的NP问题,采用回朔法 i从1开始, 先加入1,再加入2 temp=[1,2] 然后去掉2 加3 temp=[1,3]

【leetcode】Anagrams (middle)

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. anagrams 的意思是两个词用相同的字母组成  比如 “dog" "god" 思路: 把单词排序 如 dog 按字母排序变为 dgo 用unordered_map<string, int> 记录排序后序列第一次出现时,字符串在输入st

【leetcode】Permutations (middle)

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 求没有重复数字的全排列 思路:用的标准回溯法,一次AC class Solution { public: vector<vector<int>

【LeetCode】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,

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, 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,2], a solutio

【leetcode】Pascal&#39;s Triangle I &amp; II (middle)

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] ] 思路:杨辉三角,直接按规律生成即可 vector<vector<int> > generate(int numRows) { vector<vector<int>>

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu