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


相关推荐

  • Git 设置用户名、邮箱和SSH密钥

    Git 设置用户名、邮箱和SSH密钥当我们安装好Git之后第一件事就应该是设置用户名还有邮箱,那么下面就说说怎么设置吧~查看#查看当前项目中的设置gitconfig-l#查看git全局的设置gitconfig-l–global设置按照上面说的查看方法可以得知,设置用户名和邮箱同样可是全局还有单独项目,区分就是在参数中是否加上–globalgitconfig–globaluser.name’admin’gitconfig–globaluser.email’admin@gmail.com

    2022年9月7日
    0
  • 嘘…偷偷教你破解“朋友圈三天可见”「建议收藏」

    嘘…偷偷教你破解“朋友圈三天可见”「建议收藏」点击上方[全栈开发者社区]→右上角[…]→[设为星标⭐]在微信公开课上,腾讯高级执行副总裁、微信事业群总裁张小龙说:朋友圈状态设置三天可见的人数超过了一亿人,这个开关是微信里使用率最…

    2022年4月28日
    93
  • Centos7安装MySQL详细步骤

    Centos7安装MySQL详细步骤Centos7安装MySQL详细步骤首先在虚拟机中安装一个Centos7(VM虚拟机安装Centos7)1.1MySQL安装1.1.1下载wget命令yum-yinstallwget1.1.2在线下载mysql安装包wgethttps://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm1.1.3安装MySQLrpm-ivhmysql57-community-release-el7-8.noar

    2022年6月13日
    23
  • mysql倒序截取字符串_MySQL数据库之mysql截取字符串与reverse函数

    mysql倒序截取字符串_MySQL数据库之mysql截取字符串与reverse函数本文主要向大家介绍了MySQL数据库之mysql截取字符串与reverse函数,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。这个网页上很多知识点,可以学习下,关于mysql的函数,也可以作为API查询:这里只说下mysql的截取函数和reverse函数:MySQL字符串截取函数:left(),right(),substring(),substring_index()…

    2025年6月16日
    0
  • [面试] Golang 面试题

    [面试] Golang 面试题本文章收录于:后端工程师面试题目总结(提供参考答案)目录1.make与new的区别2.简要描述go中的main和init函数的区别3.下面的代码输出什么,若会报错报什么错?4.这段代码会输出什么?5、简述channel和mutex锁机制的原理异同与使用场景6、sync.WaitGroup的使用场景?7、写一段闭包代码,阐述其作用8、执行这段代码会发生什…

    2022年6月29日
    26
  • icmp回复报文_常见的ICMP报文

    icmp回复报文_常见的ICMP报文常见的ICMP报文相应请求我们用的ping操作中就包括了相应请求(类型字段值为8)和应答(类型字段值为0)ICMP报文。过程:一台主机向一个节点发送一个类型字段值为8的ICMP报文,如果途中没有异常(如果没有被路由丢弃,目标不回应ICMP或者传输失败),则目标返回类型字段值为0的ICMP报文,说明这台主机存在。目标不可达,源抑制和超时报文这三种报文的格式是一样的。(1)目标不可到达报文(类型值为3…

    2022年5月1日
    123

发表回复

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

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