博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
77. 组合
阅读量:4027 次
发布时间:2019-05-24

本文共 708 字,大约阅读时间需要 2 分钟。

给定两个整数 n 和 k,返回 1 ... 中所有可能的 k 个数的组合。

示例:

输入: n = 4, k = 2输出:[  [2,4],  [3,4],  [2,3],  [1,2],  [1,3],  [1,4],]

思路:dfs,前面做过好多类似的题目了。

深搜来做。

1、如果得到的当前元素个数等于k,返回。

2、每一层往下搜索的时候,从这个数的后面开始进行搜索。

class Solution {public:    void dfs(vector
>&re, vector
temp, int k, int n, int index){ if(temp.size()==k){ re.push_back(temp); return; } for(int i=index; i<=n; ++i){ temp.push_back(i); dfs(re, temp, k, n, i+1); temp.pop_back(); } } vector
> combine(int n, int k) { vector
>re; vector
temp; dfs(re, temp, k,n, 1); return re; }};

 

转载地址:http://heabi.baihongyu.com/

你可能感兴趣的文章
wpa_supplicant控制脚本
查看>>
rfkill: WLAN hard blocked
查看>>
gstreamer相关工具集合
查看>>
arm 自动升级脚本
查看>>
RS232 四入四出模块控制代码
查看>>
gstreamer插件之 videotestsrc
查看>>
autoupdate script
查看>>
linux 驱动开发 头文件
查看>>
/etc/resolv.conf
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
linux sfdisk partition
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
电平触发方式和边沿触发的区别
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>
带WiringPi库的交叉编译如何处理一
查看>>