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> > ans;
 4     vector<int> vi;
 5     int k_start,n_start;
 6     void DFS(int Count,int start)
 7     {
 8         if(Count== k_start)
 9         {
10             ans.push_back(vi);
11             return;
12         }
13         for(int i=start;i<n_start;i++)
14         {
15             vi[Count] = i+1;
16             DFS(Count+1, i+1);
17         }
18     }
19     vector<vector<int> > combine(int n, int k) {
20         ans.clear();
21         vi.resize(k);
22         k_start = k;
23         n_start = n;
24         DFS(0,0);
25         return ans;
26     }
27 };

LeetCode:Combinations 题解

时间: 2024-10-06 06:32:16

LeetCode:Combinations 题解的相关文章

LeetCode: LetterCombinations 题解

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: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 [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: palindromes 题解

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

[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: plusOne 题解

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意: 给定一个由一组数字构成的数组表示的非负整数, 对这个数进行加一操作,并将结果数组返回. 数组的首元素存储的是该非负整数的最高位. 本题比较简

[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

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