leetcode #77 in cpp[通俗易懂]

leetcode #77 in cpp[通俗易懂]Giventwointegers n and k,returnallpossiblecombinationsof k numbersoutof1… n.Forexample,If n =4and k =2,asolutionis:[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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++;
            }
        }
    }
};

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188316.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据分析师面试准备

    数据分析师面试准备数据分析师面试准备惊醒。突然发现再要一个月就要过年了,过了年再过个两周就三月了。三月……又到了招聘季。

    2022年6月3日
    33
  • (2019)OCP 12c 062考试题库出现大量新题-4

    (2019)OCP 12c 062考试题库出现大量新题-4

    2021年7月5日
    89
  • c语言浮点数输出格式的控制,c语言输出格式控制「建议收藏」

    c语言浮点数输出格式的控制,c语言输出格式控制「建议收藏」1.转换说明符%a(%A)浮点数、十六进制数字和p-(P-)记数法(C99)%c字符%d有符号十进制整数%f浮点数(包括float和doulbe)%e(%E)浮点数指数输出[e-(E-)记数法]%g(%G)浮点数不显无意义的零”0″%i有符号十进制整数(与%d相同)%u无符号十进制整数%o八进制整数e.g.0123%x(%X)十六进制整数0f(0F)e.g…

    2022年7月24日
    7
  • fastText原理和文本分类实战,看这一篇就够了[通俗易懂]

    fastText原理和文本分类实战,看这一篇就够了[通俗易懂]fastText原理篇一、fastText简介fastText是一个快速文本分类算法,与基于神经网络的分类算法相比有两大优点:1、fastText在保持高精度的情况下加快了训练速度和测试速度2、fastText不需要预训练好的词向量,fastText会自己训练词向量3、fastText两个重要的优化:HierarchicalSoftmax、N-gram二、fastText模型架构…

    2022年6月11日
    128
  • intentservice使用(Intention)

    IntentService,更好用的Service说起IntentService就需要先了解一下Service。Service是长期运行在后台的应用程序组件。Service不是一个单独的进程,它和应用程序在同一个进程中,Service也不是一个线程,它和线程没有任何关系,所以它不能直接处理耗时操作。如果直接把耗时操作放在Service的onStartCommand()中,…

    2022年4月18日
    35
  • Spring Batch示例教程

    Spring Batch示例教程SpringBatch示例教程欢迎使用SpringBatch示例。SpringBatch是一个用于执行批处理作业的弹簧框架模块。我们可以使用spring批处理来处理一系列作业。目录[隐藏]1SpringBatch示例 1.1SpringBatch示例 1.2Spring批处理示例目录结构 1.3SpringBatchMaven依赖项 …

    2022年5月28日
    102

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号