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


相关推荐

  • java中的onresume_java – 直接onResume()调用的替代方法

    java中的onresume_java – 直接onResume()调用的替代方法我正在重写我的Android应用以消除对onResume()的直接调用.我的应用程序目前在onResume()内部完成大部分工作,然后发布显示,这是onResume()的结束.@OverridepublicvoidonResume(){super.onResume();//getcurrentdateandtime,//anddetermineifdaylightsav…

    2022年6月2日
    34
  • 八数码问题简单解决办法

    八数码问题简单解决办法问题分析:八数码问题是一个经典的BFS问题,把棋局看成一个状态图,共有9!种状态。从初始棋局开始,每次转移到下个状态,直到目标棋局为止。仔细分析可知,八数码的关键是判重,如果不去除重复状态,程序会产生很多无效状态,从而复杂度大大增加解决算法:BFS+Cantor案例分析:(0表示空格所在位置)初始棋局:|1|2|3||0|8|4||7|6|5|目标棋局:|1|0|…

    2022年7月12日
    27
  • NAT MASQUERADE

    NAT MASQUERADESNAT是sourcenetworkaddresstranslation的缩写即源地址目标转换比如,多个PC机使用ADSL路由器共享上网,每个PC机都配置了内网IP。PC机访问外部网络的时候,路由器将数据包的报头中的源地址替换成路由器的ip。当外部网络的服务器比如网站web服务器接到访问请求的时候,他的日志记录下来的是路由器的ip地址,而不是pc机的内网ip。这是因为,这个服务器收到的数…

    2022年6月29日
    33
  • 全网最全性能优化总结!!(冰河吐血整理,建议收藏)「建议收藏」

    全网最全性能优化总结!!(冰河吐血整理,建议收藏)「建议收藏」性能优化一般包含:数据聚合优化、资源冲突优化、算法优化、JVM优化、复用优化、计算优化和快速优化,冰河吐血整理,建议大家收藏!!

    2022年8月22日
    7
  • 51单片机通过WIFI模块ESP8266控制LED灯

    51单片机通过WIFI模块ESP8266控制LED灯一 系统方案手机 APP 通过 ESP8266WIFI 模块与 51 单片机通信控制 LED 灯的开关 下位机由单片机 ESP8266 模块和 LED 灯组成 上位机由 Android 手机 APP 承担 我们在 APP 上发送 LED 灯的开关控制指令 ESP8266 将收到的数据发送给单片机 从而实现对 LED 灯进行开关控制 设计好的实物是这个样子 二 硬件设计 ESP8266 模块作为一个透传模块使用 RXD

    2025年7月11日
    4
  • Android退出APP 并杀掉相关的所有进程

    Android退出APP 并杀掉相关的所有进程代码如下:ActivityManagermActivityManager=(ActivityManager)AppApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE);List<ActivityManager.RunningAppProcessInfo>m…

    2022年7月17日
    22

发表回复

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

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