Leetcode dfs Combinations

Combinations

Total Accepted: 18327 Total
Submissions: 60479My Submissions

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],
]

题意:给定两个数字 n 和 k,给出从1...n数字中取 k个的所有可能组合

思路:dfs

递归的深度为 k,第 i 层的第j个节点有  n - i - j 个选择分支

复杂度:时间 O(n^k),空间O(k)

vector<vector<int> > res;
int nn, kk;
void dfs(int cur, int start, vector<int> &path){
	if(cur == kk ){
		res.push_back(path);
	}
	for(int i = start; i <= nn; i++){
		path.push_back(i);
		dfs(cur + 1, i + 1, path);
		path.pop_back();
	}
}

vector<vector<int> > combine(int n, int k){
	nn = n, kk = k;
	vector<int> path;
	dfs(0, 1, path);
	return res;
}

时间: 2024-10-05 21:07:47

Leetcode dfs Combinations的相关文章

LeetCode: Letter Combinations of a Phone Number [018]

[题目] 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:Letter Combinations of a Phone Number 手机键盘字母映射

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" Outp

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], ]DFS,递归 1 class Solution { 2 public: 3 vector<vector<int> > an

Leetcode dfs Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 15964 Total Submissions: 60700My Submissions Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephon

Palindrome Partitioning[leetcode] DFS以及DP的解法

第一种方法是DFS,将所有可能的前缀找到,递归调用partition(剩余字符串) 复杂度为O(2^n) 代码如下: vector<vector<string>> partition(string s) { vector<vector<string>> res; vector<string> patition; if (s.size() == 0) return res; partition(s, patition, res); return r

Java for LeetCode 077 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,JAVA实现如下: public static void main(String args[]) { List<

LeetCode: Letter Combinations of a Phone Number 解题报告

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

LeetCode——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", &q

[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