LeetCode(77) Combinations
题目
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~n-1)中k-1个数的所有组合,然后每个组合中加入元素n;
- 第二部分,求(1~n-1)中k个数的所有组合;
上述两部分合并便可以得到1~n中k个数的所有组合。
算法设计需要注意几个问题,避免不必要的bug:
- 判断 n<=0 时,组合为空
- 判断 n<k 时(也就包括了 k<=0 的情况),组合均为空
- 判断 k==1 时, 1−n 每个元素为一个组合,返回 n 个组合
- 判断 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
}
};
LeetCode(77) Combinations的更多相关文章
- LeetCode(77):组合
Medium! 题目描述: 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3] ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Ubuntu 18.04 LTS 安装后 各种问题以及解决方案
1. root的初始密码,默认是不知道的,需要进行设置 a. 进入终端自己的用户 b. 输入 sudo passwd回车 c. 输入新密码,回车,重复,回车,搞定 d. su 一下,就可以了 2. ...
- UIImageView 使图片圆形的方法
UIImageView 圆形的两种方法 1.cornerRadius (tableView,collectionView尽量避免使用,影响性能) //想要圆角 cornerRadius必须是 imag ...
- mysql架构解析
架构 mysql是cs架构,在服务端可以启动该mysqld服务,mysqld进程默认监听在tcp:3306.在客户端使用命令行工具mysql或者图形化工具navicat for mysql进行远程连接 ...
- LightOj 1088 - Points in Segments (二分枚举)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1088 题目描述: 给出一个n位数升序排列的数列,然后q个查询,每个查询问指定 ...
- HDU - 6058 Kanade's sum
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...
- DP+高精度 URAL 1036 Lucky Tickets
题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...
- .net服务端生成二维码
mvc4 net4.0 1.引用附件的DLL文件 2.两个函数即可 #region 生成二维码 public ActionResult getQrCode() { using (var ms = ...
- sass+compass起步
前言:Sass is an extension of CSS that adds power and elegance to the basic language. It allows you to ...
- java 获取ip地址
1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...
- uva1352 Colored Cubes LA3401
白书第一章例题8 好麻烦! 正方体每面编号为0-5,那么根据顶点和正面,就能确定形态.一共6*4=24种形态. P[i]表示编号i所在位置.比如P[1]=3,表示第二面转到了第四面. 就可以表示出所有 ...