[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 {
public:
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int> > res;
        if(n<1 || k<1)
          return res;

        vector<int> v,v0;
        for(int i=1;i<=n;i++){
            v.push_back(i);
            res.push_back(v);
            v.clear();
        }//end for

        while(!res.empty()){
            v = res[0];
            res.erase(res.begin());
            if(v.size() == k){
                res.push_back(v);
                break;
            }else{
                v0 = v;
                for(int i=1;i<=n;i++){
                    if(find(v.begin(),v.end(),i)==v.end()){
                        v.push_back(i);
                        sort(v.begin(),v.end());
                        if(find(res.begin(),res.end(),v)==res.end())
                           res.push_back(v);
                        v = v0;
                    }
                }//end for
            }

        }//end while
        return res;
    }//end func
};

[LeetCode] Combinations (bfs)

时间: 2024-08-04 22:37:29

[LeetCode] Combinations (bfs)的相关文章

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 生成组合

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】BFS(共43题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较对象是 左子树的左儿子和右子树的右儿子, 左子树的右儿子和右子树的左儿子.不要搞错. // 直接中序遍历的话会有错的情况,最蠢的情况是数字标注改一改.. 1 /** 2

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

[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:若不等于