[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<int> >result;
    set<vector<int> > myset;
    int kNum = 0;

    void addSolution(){
        vector<int> tmp(record.begin(),record.end());
        sort(tmp.begin(),tmp.end());
        if(myset.find(tmp) == myset.end()){
            result.push_back(tmp);
            myset.insert(tmp);
        }
    }
    void subCombinationSum(vector<int> &cadidates,int bpos,vector<int> &base, int k){
        if(kNum == k){
            addSolution();
        }

        int size = cadidates.size();
        for(int i = bpos; i < size; i++){
            if(base[i] == 1)
                continue;

            record.push_back(cadidates[i]);
            base[i] = 1;
            kNum++;
            subCombinationSum(cadidates,<span style="color:#FF0000;"><strong>i+1</strong></span>,base,k);  //i+1  避免重复
            record.pop_back();
            base[i] = 0;
            kNum--;
        }
    }

    vector<vector<int> > combine(int n, int k) {
        vector<int> candidates(n);
        for(int i = 0; i < n; i++)
            candidates[i] = i+1;
        vector<int> base(candidates.size(),0);
        subCombinationSum(candidates,0,base,k);
        return result;
    }
时间: 2024-10-14 03:33:42

[leetcode]Combinations的相关文章

LeetCode: Combinations [077]

[题目] Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such

[LeetCode] Combinations [38]

题目 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 Solutio

[LeetCode] Combinations (bfs)

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], ] 方法:把用queue实现bfs,改为用vector自己实现bfs,没有用额外的内存来存储中间值: class Solution {

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], ] 题解分析: 典型的dfs class Solution { public: vector<vector<

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], ] 原题链接:https://oj.leetcode.com/problems/combinations/ 题目:给定两个整数n和k,返

[leetcode] Combinations @ Python [ask for help]

https://oj.leetcode.com/problems/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],] 思路: 稍后想通了再补充. 代码: cla

[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], ] Hide Tags Backtracking 这题是回溯题目,做好递归控制便可以了. #include <iostream> #in

[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], ] 题意:给定1...n个数,求出每k个数的组合情况. 思路:使用DFS.定义中间数组变量,每当其大小为k时,将其存入结果res:若不等于

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],] SLOUTION 1: 一段时间不写真心容易不记得. 这是一道典型的模板题.以下是模板写法. 1.