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


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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • android之ContentObserver内容观察者的使用

    ContentObserver——内容观察者,目的是观察(捕捉)特定Uri引起的数据库的变化,继而做一些相应的处理,它类似于   数据库技术中的触发器(Trigger),当ContentObserver所观察的Uri发生变化时,便会触发它。(1)注册:    public final void  registerContentObserver(Uri uri, boolean noti

    2022年3月11日
  • 银行家算法 C语言实现

    银行家算法 C语言实现C语言实现银行家算法这几天老师要求使用C语言实现银行家算法,数据可以自定义。想来想去还是照着书现成的数据来模拟一下。教材使用的是西安电子科大出版社的《计算机操作系统》汤小丹第四版。模拟数据使用的是P121页第4题的数据。听到老师布置题目的第一时间我还是有点懵,看了下书更懵了,这条条框框的判断条件怎么这么多。。沉下心来慢慢看,其实还是挺简单的算法。/*Author:Cnkizy…

    2022年6月10日
  • 计算机网络知识汇总(超详细整理)

    计算机网络知识汇总(超详细整理)为了准备期末考试,同时也是为了之后复习方便,特对计算机网络的知识进行了整理。本篇内容大部分是来源于我们老师上课的ppt。而我根据自己的理解,将老师的PPT整理成博文的形式以便大家复习查阅,同时对于一些不是很清楚的地方,我去查阅了相关资料进行补充,当然也会有部分个人看法夹带其中来帮助大家理解。

    2022年6月14日
  • Tomcat如何打破双亲委派机制实现隔离Web应用的?

    Tomcat通过自定义类加载器WebAppClassLoader打破双亲委托,即重写了JVM的类加载器ClassLoader的findClass方法和loadClass方法,这样做的目的是优先加载Web应用目录下的类。除此之外,你觉得Tomcat的类加载器还需要完成哪些需求呢?或者说在设计上还需要考虑哪些方面?我们知道,Tomcat作为Servlet容器,它负责加载我们的Servlet类,此外它还负责加载Servlet所依赖的JAR包。并且Tomcat本身也是一个Java程序,因此它需要加载自己的类和依赖

    2022年4月7日
  • pyecharm激活码[在线序列号]

    pyecharm激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
  • 深入解析Linux Platform_device 及驱动

    深入解析Linux Platform_device 及驱动[导读]前文分析了Linux设备驱动的驱动模型,本文来聊聊Platform_driver/Platform_device这个类。做嵌入式Linux的驱动,这个也是绕不开的,所以来学习分析总结一下。

    2022年7月3日

发表回复

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

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