[LeetCode] Combinations [38]
称号
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],
]
解题思路
组合问题,老思路---递归加循环,这个是组合里面比較简单的。
代码实现
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ret;
helper(k, 1, n, vector<int>(), ret);
return ret;
}
void helper(int k, int start, int n, vector<int> part, vector<vector<int> > &ret){
if(k==part.size()){
ret.push_back(part);
return;
}
for(int i=start; i<=n; ++i){
part.push_back(i);
helper(k, i+1, n, part, ret);
part.pop_back();
}
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
版权声明:本文博主原创文章。博客,未经同意不得转载。
[LeetCode] Combinations [38]的更多相关文章
- 【LeetCode算法-38】Count and Say
LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode——Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- leetcode — combinations
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- [leetcode]Combinations @ Python
原题地址:https://oj.leetcode.com/problems/combinations/ 题意:组合求解问题. 解题思路:这种求组合的问题,需要使用dfs来解决. 代码: class S ...
- [LeetCode] Combinations (bfs bad、dfs 递归 accept)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- leetcode第38题--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- 【一天一道LeetCode】#38. Count and Say
一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
随机推荐
- 用链表实现栈----《数据结构与算法分析----C语言描述》
一.头文件: #ifndef _STACK_LINK_H_ #define _STACK_LINK_H_ struct stack_record; typedef struct stack_recor ...
- 【leetcode】Candy(python)
题目要求的比它的邻居比自己奖励,因此,我们有最少一个多的.所有我们可以找到所有的坑,凹坑例如,存在以下三种情况. 找到全部的凹点后,我们就能够从凹点处開始向左右两个方向依次查找递增序列.当中每一个高的 ...
- 利用 C++ 单向链表实现队列
利用C++ 单向链表实现数据结构队列,其实和上一篇基本内容相同,仅仅是插入的时候在链表的尾部插入,取元素都是一样的,都从头部取. #pragma once #include "stdio.h ...
- ECshop lib_base.php on line 1241 错误解决方法
ECSHOP做的一个网站,突然报这个错误,整个网站打不开,后来找了很久,终于找到这个方法,亲测可用 Notice: Undefinedvariable: data in D:\wwwroot\KISS ...
- Qt4.8在Windows下的三种编程环境搭建
Qt4.8在Windows下的三种编程环境搭建 Qt的版本是按照不同的图形系统来划分的,目前分为四个版本:Win32版,适用于Windows平台:X11版,适合于使用了X系统的各种Linux和Unix ...
- java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor An ExecutorService that executes each submitted task using o ...
- 64地点 Windows 8/7 根据系统 32地点PLSQL 耦合 64 地点 Oracle 11g
64地点 Windows 8/7 根据系统 32地点PL/SQL 耦合 64 地点 Oracle 11g 说明:安装后Oracle的 oci.dll 是64位的,而32位应用程序 PL/SQL ...
- java连接数据库——JDBC连接数据库
DBUtil.java // 数据库操作文件 package com.bjpowernode.jdbc.util; import java.io.File; import java.io.File ...
- MySQL和Oracle开发差异
1) 数据类型差异 Oracle MySQL 注释 单独创建序列来实现 自动增长的数据类型 varchar2 varchar number tinyint,smallint,mediumint,in ...
- UML之轻松入门(3)-SRP做好厨子,让别人编程去吧
一个厨子能够做出一手好菜,或许他是新东方毕业的或者是祖传秘方.你让他做上一桌佳肴那是简单.快乐而又高效的,然而让他编程就会成为一种苦恼并且让人想不通的一件事.或许这个比喻不是非常恰当,可是对 ...