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 window in S that covers all characters in T, return the emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

【题意】

给定数字n和k, 返回所有由1,2,3...,n数组成k元组合

【思路】

递归

【代码】

class Solution {
public:

    void dfs(vector<vector<int> >&result, vector<int>combination, int kth, int k, int curVal, int n){
        // combination 已经生成的前kth-1个数
        // kth      本次将要生成组合中的第kth个数
        // k        组合规模
        // curVal   本次将加入组合中的数,即第kth个数
        // n        候选数的总规模
        combination.push_back(curVal);
		if(kth==k)result.push_back(combination);
		else{
			for(int val=curVal+1; val+(k-kth-1)<=n; val++){
				dfs(result, combination, kth+1, k, val, n);
			}
		}
    }

    vector<vector<int> > combine(int n, int k) {
        vector<vector<int> >result;
        if(k>n || n<0 || k<1)return result;

        vector<int> combination;
        for(int val=1; val<=n-(k-1); val++){
            dfs(result, combination, 1, k, val, n);
        }
        return result;
    }
};

LeetCode: Combinations [077],布布扣,bubuko.com

时间: 2024-08-05 23:12:21

LeetCode: Combinations [077]的相关文章

[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], ] 基本思想: 回溯法 代码: 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:若不等于

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.