这是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 组合项的更多相关文章

  1. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  2. combinations(组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. 【LeetCode每天一题】Combinations(组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  4. leetCode 77.Combinations (组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. [Leetcode] combinations 组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. 077 Combinations 组合

    给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.例如,如果 n = 4 和 k = 2,组合如下:[  [2,4],  [3,4],  [2,3],  [1,2],  [ ...

  7. [leetcode]77. Combinations组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  8. Leetcode77. Combinations组合

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

  9. LeetCode(46):全排列

    Medium! 题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [ ...

随机推荐

  1. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  2. MySQL 全文搜索支持, mysql 5.6.4支持Innodb的全文检索和类memcache的nosql支持

    背景:搞个个人博客的全文搜索得用like啥的,现在mysql版本号已经大于5.6.4了也就支持了innodb的全文搜索了,刚查了下目前版本号都到MySQL Community Server 5.6.1 ...

  3. nginx服务器的网站权限问题

    有时候我们的网站根目录会从一个目录迁移到另一个目录,如果我们服务器使用的是nginx或者Apache,我们一般会配置好网站根目录后然后往直接把网站解压或者上传到根目录中,这样引起的问题是无法对对文件进 ...

  4. codeforces B. Flag Day 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/B 题目意思:输入n个人和m场舞蹈,给出每场舞蹈(只有3个人参与)中参与的舞者的编号,你需要为这些舞 ...

  5. [MAC ] Mac-OSX下安装Git

    转载自 : http://www.cnblogs.com/shanyou/archive/2011/01/30/1948088.html Mac-OSX下安装Git是一件很简单的事,我们可以下载一个安 ...

  6. vs 附加包含目录属性

    如果是在属性页里头添加了路径,则当程序拷贝到其他电脑上头的话,这个包含目录仍然存在,这就是与添加环境变量的区别.如果是通过添加环境变量配置的路径,则换了台电脑,这个路径就没有了,需要重新配置.

  7. Android实现高仿QQ附近的人搜索展示

    本文主要实现了高仿QQ附近的人搜索展示,用到了自定义控件的方法 最终效果如下 1.下面展示列表我们可以使用ViewPager来实现(当然如果你不觉得麻烦,你也可以用HorizontalScrollVi ...

  8. Android之ExpandableListView

    ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...

  9. Interger 与 int

    int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...

  10. 一、HTML和CSS基础--HTML+CSS基础课程--第4部分

    第七章 CSS的继承.层叠和特殊性 继承:CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代. 特殊性
权值的规则: 标签 ...