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 ...
随机推荐
- java 反向工具类
1.ReflectUtil.java package com.example.scansell; import android.util.Log; import java.lang.reflect.C ...
- android动画(1)各种动画属性表,简单代码,xml配置
1.动画的属性表 1.1 常用属性 它们可以通过代码setXXX,getXXX,也可在xml中配置. 属性[类型] 功能 备注 Duration[long] 属性为动画持续时间 时间以毫秒为单位 fi ...
- cpp extern 关键字用法
语法说明: extern 可以置于变量或者函数前,以标示变量或者函数的在别的文件中定义,提示编译器遇到此变量和函数后,在其他模块中寻找其定义.此外extern也可用来进行链接指定. 即 extern ...
- selenium2+python自动化2-元素定位
嘻嘻,书接上回,接着唠,这里先补充一下自动化要掌握的四个步骤吧:获取元素.操作元素.获取返回值.断言(返回结果与期望结果是否一致),最后就是自动化测试报告的生成.这一片主要讲一下如何进行元素定位.元素 ...
- Android Studio编译开源项目(含NDK开发)常见报错
1.未设置NDK的路径 Error:Execution failed for task ':library:ndkBuild'. > A problem occurred starting pr ...
- UESTC - 878 温泉旅店 二维费用背包问题
http://acm.uestc.edu.cn/#/problem/show/878 设dp[i][j][k]表示在前i个数中,第一个得到的异或值是j,第二个人得到的异或值是k的方案数有多少种. 因为 ...
- RAID 0、1、5、1+0总结
RAID(Redundant Array Of Independent Disk,独立磁盘冗余阵列),可以提供比普通磁盘更快的速度.更高的安全性,生产环境中服务器在安装时一般都会做RAID,RAID的 ...
- 【转】Java中创建对象的5种方式
Java中创建对象的5种方式 作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如Spring去创建对象.然而这里有很多创建对象的方法,我们会在这篇文章中学到. Java中有 ...
- 事件对象(示例、封装函数EventUtil())
事件对象 什么是事件对象? 在触发DOM上的事件时都会产生一个对象. 事件对象event 1.DOM中的事件对象 (1)\type属性用于获取事件类型 (2)\target属性用于获取事件目标 (3) ...
- 后TOS时代的码头数字化生产力
之前看过一篇文章是,是INFORM的副总裁写的关于以TOS外挂模式提升码头生产效能的文章.文章对外挂模式的总结挺好的,我最近也一直从事这块的工作,以此文梳理一下前面的经验,记录一下自己的感想. TOS ...