leetcode #77 in cpp

原创 2016年06月01日 22:50:51

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


Solution:

It is like permutation(#46 and #47). Thus we still use recurrence to search for combinations. 

given a list called member that contains i numbers in current combination:

if i == k, push member into our final result

else

for each num[j] in member,  i <= j < n;

1. push num[j] into member. 

2. recursively find combination of (num[j+1.....n], k-(i+1) ) 

For example, given [1,2,3,4] and k = 2, 

we start from 1, member is []

push 1 into member. Now member is [1], and we just collect 1 number. We need to collect 1 more.  Go to next recurrence. 

Our current position is 0, we should loop from position 1 to n-1:

1. position 1 which is 2. put 2 into member and member becomes [1,2]. Now we have 2 numbers. put member into final result.

2. position 2 which is 3. put 3 into member and member becomes [1,3]. Now we have 2 numbers, put member into final result

3. position 3 is 4. put 4 into member and member becomes [1,4]. Put member into final result. 

recurrence starting from 1 ends


we start from 2, member is []

push 2 into member. Now member is [2], and we just collect 1 number. We need to collect 1 more.  Go to next recurrence. 

Our current position is 1, we should loop from position 2 to n-1:

1. position 2 which is 3. put 3 into member and member becomes [2,3]. Now we have 2 numbers. put member into final result.

2. position 3 which is 4. put 4 into member and member becomes [2,4]. Now we have 2 numbers, put member into final result

recurrence starting from 2 ends


we start from 3, member is []

push 3 into member. Now member [3], and we just collect 1 number. We need to collect 1 more. Go to next recurrence

Our current position is 2, we should loop from position 3 to n-1:

1. position 3 which is 4. put 4 into member and member becomes [3,4]. Now we have 2 numbers. put member into final result.

recurrence starting from 3 ends


we start from 4, member is []

push 4 into member. Now member [4], and we just collect 1 number. We need to collect 1 more. Go to next recurrence

Our current position is 3, we should loop from position 4 to n-1, but 4 > 3. Thus we terminate the recurrence from 4. member is not pushed. 


Code:

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> res;
        
        for(int i = 1; i <=n ; i ++){
            find_combine(i,n, k-1,vector<int>(), res);
        }
        return res;
    }
    void find_combine(int i, int n, int left, vector<int> member, vector<vector<int>> &res){
        member.push_back(i);
        if(left == 0){
            res.push_back(member);
            return;
        }
        else{
            i++;
            while(i<=n){
                find_combine(i, n, left - 1, member, res);
                i++;
            }
        }
    }
};


Leetcode 77. Combinations 组合算法 解题报告

1 解题思路适逢期末,事情比较多,而且最近心好烦,所以更新不规律了一点,“大姨爹”来了吧。所谓组合问题,可以理解为在1..n个数字里面,选k个数字,那么能选出多少种呢?注意,这里只要求组合,不要求排列...
  • MebiuW
  • MebiuW
  • 2016年05月26日 00:18
  • 1079

LeetCode 77. Combinations 解题报告

77. Combinations My Submissions Question Total Accepted: 67261 Total Submissions: 201946 ...
  • bruce128
  • bruce128
  • 2016年02月18日 14:03
  • 1898

leetcode 77. Combinations-排列|递归|非递归|Java|Python

原题链接:77. Combinations 【思路-Java、Python】 采用回溯算法。这是一道 NP 难问题,时间复杂度没办法提高,用一个循环递归处理子问题,问题的终止条件是每个组合中的元素个数...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016年06月02日 10:43
  • 1893

LeetCode 77 Combinations (Python详解及实现)

【题目】 Given two integers n and k, return allpossible combinations of k numbers out of 1 ... n.   F...
  • yangjingjing9
  • yangjingjing9
  • 2017年08月08日 13:50
  • 365

LeetCode-77 Combinations(组合)

LeetCode-77 Combinations(组合) Given two integers n and k, return all possible combinations of k numb...
  • u011035622
  • u011035622
  • 2015年05月02日 12:06
  • 670

[leetcode-77]Combinations(java)

问题描述: Given two integers n and k, return all possible combinations of k numbers out of 1 … n.For ex...
  • zdavb
  • zdavb
  • 2015年08月05日 20:57
  • 549

LeetCode77——Combinations

LeetCode77——Combinations 题意: 两个数字n和k,找出所有这样的组合: 1.组合中有k个数字 2.组合是递增 3.组合中的数字是{1,2,3,....n} 的子集 ...
  • zhangxiao93
  • zhangxiao93
  • 2015年11月17日 21:21
  • 1450

leetcode 77

返回n个数中k的排列,用到了递归的方法。 class Solution(object): def __init__(self): self.l = [] def co...
  • u010041824
  • u010041824
  • 2017年03月27日 22:14
  • 335

LeetCode Solution(持续更新,java>c++)

Title Add Date AC Rates
  • disappearedgod
  • disappearedgod
  • 2014年04月13日 21:52
  • 8830

[leetcode] 【分治法】 50. Pow(x, n)

Implement pow(x, n). 题意 实现幂函数。 题解 使用分治法求解。 分:将n分成n/2  直到n=0时,返回1; 治:对n为偶数,返回两数相乘的结果,奇数再乘多一个x...
  • u014654002
  • u014654002
  • 2016年06月21日 00:37
  • 381
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #77 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)