题目

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

分析

数学领域的组合问题,求1~n中k个数的组合问题。

看到题目的条件反射就是暴力法依次列举解决问题,实际上这条道路应该是行不通的,即便是得到正确结果,时间复杂度也会很高。

仔细思考,这道题目可以用动态规划的思想解决:

1~n中k个数的所有组合一定是由两部分组成:

  1. 第一部分,求(1~n-1)中k-1个数的所有组合,然后每个组合中加入元素n;
  2. 第二部分,求(1~n-1)中k个数的所有组合;

上述两部分合并便可以得到1~n中k个数的所有组合。

算法设计需要注意几个问题,避免不必要的bug:

  1. 判断 n<=0 时,组合为空
  2. 判断 n<k 时(也就包括了 k<=0 的情况),组合均为空
  3. 判断 k==1 时, 1−n 每个元素为一个组合,返回 n 个组合
  4. 判断 n==k 时,此时只有一个组合,包括元素 1−n

AC代码

class Solution {
public:
vector<vector<int>> combine(int n, int k) { if (n <= 0 || n < k)
return vector<vector<int>>(); //保存结果
vector<vector<int>> ret;
if (k == 1)
{
for (int i = 1; i <= n; i++)
{
vector<int> v(1,i);
ret.push_back(v);
}//for return ret;
}//if
if (n == k)
{
vector<int> v;
for (int i = 1; i <= n; i++)
{
v.push_back(i);
}//for
ret.push_back(v); return ret;
}//if
else{
//由两部分组成,第一部分为 1~n-1 中k-1个数的组合,每个组合加入元素n
vector<vector<int>> tmp = combine(n - 1, k - 1);
int len = tmp.size();
for (int i = 0; i < len; i++)
{
tmp[i].push_back(n);
ret.push_back(tmp[i]);
}//for //第二部分,1~n-1中 k个数的组合,两部分合并得到最终结果
tmp = combine(n - 1, k);
len = tmp.size();
for (int i = 0; i < len; i++)
{
ret.push_back(tmp[i]);
}//for return ret;
}//else
}
};

GitHub测试程序源码

LeetCode(77) Combinations的更多相关文章

  1. LeetCode(77):组合

    Medium! 题目描述: 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3] ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  8. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. Zookeeper的多节点集群详细启动步骤(3或5节点)

    分为 (1)分别去3或5节点上去启动Zookeeper进程 (2)自己写个脚本,直接在主节点上去启动Zookeeper进程. (1)分别去3或5节点上去启动Zookeeper进程 第一步: [hado ...

  2. 自定义view(13)自定义属性

    1.添加attrs.xml文件 在android studio下,在res/values 下新建资源文件attrs.xml 2.添加自定义的属性 在attrs.xml中添加属性,如下.其中format ...

  3. PT2264解码心得

    PT2264解码心得 最近闲暇时间在琢磨无线RF解码程序,正好在数码之家论坛中翻出大佬的解码程序(http://bbs.mydigit.cn/read.php?tid=245739),于是乎,慢慢学习 ...

  4. 在虚拟机里安装windows或Linux系统时,安装窗口过大按钮有时点不到解决办法(图文详解)

    不多说,直接上干货! 问题详情 解决办法 很简单快捷的解决办法,就是快捷键ALT+F7,可以拖动窗口的位置. 成功!

  5. php的iconv函数中utf8与utf-8的差异

    开发中遇到一个奇怪的问题:报错如下: iconv() [<a href='function.iconv'>function.iconv</a>] : Wrong charset ...

  6. poj3204Ikki's Story I - Road Reconstruction(最大流求割边)

    链接 最大流=最小割  这题是求割边集 dinic求出残余网络 两边dfs分别以源点d找到可达点 再以汇点进行d找到可达汇点的点 如果u,v为割边 那么s->u可达 v->t可达 并且为饱 ...

  7. webform简单空间以及数据库访问

    1.简单控件 Label - 文字,编译后显示的是<span> 一说到边框:1.颜色 2.类型,比如solid实线3.width宽度Literal -里面可以承载很多东西,比如文字,比如a ...

  8. ibatis学习笔记

    步骤: 搭建配置环境:导入相关jar包 配置文件: JDBC连接属性文件 总配置文件 关于每个实体的映射(map.xml)文件 JDBC连接属性文件 jdbc.properties ## mysql ...

  9. 《Head First HTML与CSS》的HTML标签、属性

    一个标准的html5页面: <!doctype html> <html lang="zh-cmn-Hans"> <head> <meta ...

  10. 在Github上删除一个项目

    最近在Github上浏览,不小心fork了一个项目.想删除,现在记录下来. 1.点击选择fork的项目,以gubai为例 2.进入后,点击Settings 3.进入页面后,点击Delete this ...