[FollowUp] Combinations 组合项
这是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:
[
[1,2],
[1,3],
[1,4],
[2,1],
[2,3],
[2,4],
[3,1],
[3,2],
[3,4],
[4,1],
[4,2],
[4,3],
]
这题的解法其实只是在原题Combinations 组合项 的基础上做很小的改动即可,这里我们为了避免重复项,引入了visited数字来标记某个数组是否出现过,然后就是递归中的循环不是从level开始,改为每次从0开始,这样就能把所有不同的排列方式都找出来,代码如下:
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > res;
vector<int> out;
vector<int> visited(n, );
combineDFS(n, k, , visited, out, res);
return res;
}
void combineDFS(int n, int k, int level, vector<int> &visited, vector<int> &out, vector<vector<int> > &res) {
if (out.size() == k) res.push_back(out);
else {
for (int i = ; i < n; ++i) {
if (visited[i] == ) {
visited[i] = ;
out.push_back(i + );
combineDFS(n, k, level + , visited, out, res);
out.pop_back();
visited[i] = ;
}
}
}
}
};
[FollowUp] Combinations 组合项的更多相关文章
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 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. Example: I ...
- leetCode 77.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 ...
- 077 Combinations 组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.例如,如果 n = 4 和 k = 2,组合如下:[ [2,4], [3,4], [2,3], [1,2], [ ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- Leetcode77. Combinations组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...
- LeetCode(46):全排列
Medium! 题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [ ...
随机推荐
- SQL Server游标的使用
转: http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集 ...
- mysql 我的学习
安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...
- Java的switch用法
下面是switch的用法: var status = msg; switch (status) //status是表达式 { case 0:st ...
- 10件在PHP 7中不要做的事情
刚刚在园子里看到一篇特别好的文章,就拿到我的园子里分享了. 1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是 ...
- osg 中鼠标拾取线段的端点和中点
//NeartestPointNodeVisitor.h #pragma once #include <osg\Matrix> #include <vector> #inclu ...
- 开源混淆工具ProGuard配置详解及配置实例
ProGuard的作用: 1.创建紧凑的代码文档是为了更快的网络传输,快速装载和更小的内存占用. 2.创建的程序和程序库很难使用反向工程. 3.所以它能删除来自源文件中的没有调用的代码 4.充分利 ...
- 岛屿(洛谷 U5399)
题目背景 放假了,Lkw和mm到岛上旅游.阳光明媚,风景秀丽.正当Lkw和mm享受眼前这旖旎的风光时,突降大雨,小岛上开始积水,没过几分钟,水便快要触及膝盖.Lkw和mm意识到了事态的严重性,赶紧向高 ...
- 寻找代表元(codevs 2776)
题目描述 Description 广州二中苏元实验学校一共有n个社团,分别用1到n编号.广州二中苏元实验学校一共有m个人,分别用1到m编号.每个人可以参加一个或多个社团,也可以不参加任何社团.每个社团 ...
- 修剪花卉(codevs 1794)
题目描述 Description ZZ对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题. 一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉 ...
- cf455a(简单dp)
题意:给出一个长度为n的数列,元素为a1, a2, ...an:删除ai,ai+1,ai-1 可以得到ai积分,输出最多可以得到多少积分: 题解:开一个数组a存取数列,a[i]表示元素i的个数,所以删 ...