leetcode-Combinations-77

输入n和k,求1~n中任选k个数的所有组合

这题是选择不是排列,所以用dfs+回溯

还是那个问题——去重:想象一颗树,每一层相同的元素只搜索一次,下次若同一层这个元素已经做过了就跳过,具体看代码

 1 class Solution {
 2 public:
 3     void dfs(vector<int> v,int i,vector<vector<int> > &ans,vector<int> vv,int k){
 4         if(vv.size()==k){
 5             ans.push_back(vv);
 6             return;
 7         }
 8         for(int j=i+1;j<v.size();j++){
 9             if(j!=i+1&&v[j]==v[j-1]) continue;
10             vv.push_back(v[j]);
11             dfs(v,j,ans,vv,k);
12             vv.pop_back();
13         }
14     }
15     vector<vector<int> > combine(int n, int k) {
16         vector<int> v(n);
17         for(int i=0;i<n;i++) v[i]=i+1;
18         vector<vector<int> > ans;
19         vector<int> vv;
20         for(int i=0;i<v.size();i++){
21             vv.push_back(v[i]);
22             dfs(v,i,ans,vv,k);
23             vv.pop_back();
24         }
25         return ans;
26     }
27 };
时间: 2024-08-08 15:54:59

leetcode-Combinations-77的相关文章

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】#77. Combinations

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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]

&lt;LeetCode OJ&gt; 77. Combinations

Total Accepted: 69360 Total Submissions: 206274 Difficulty: Medium 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 @ 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], ] 基本思想: 回溯法 代码: 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