[LintCode] Permutations
http://www.lintcode.com/en/problem/permutations/#
Given a list of numbers, return all possible permutations.
Example
For nums =
[1,2,3], the permutations are:[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
求全排列,可以使用DFS来解决,来看代码:
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int>> paths;
if (nums.empty()) {
return paths;
}
vector<int> index;
vector<int> path;
permuteHelper(nums, index, path, paths);
return paths;
}
private:
void permuteHelper(const vector<int> &nums,
vector<int> &index,
vector<int> &path,
vector<vector<int>> &paths) {
if (path.size() == nums.size()) {
paths.push_back(path);
return;
}
for (int ix = 0; ix < nums.size(); ix++) {
if (find(index.begin(), index.end(), ix) == index.end()) {
index.push_back(ix);
path.push_back(nums[ix]);
permuteHelper(nums, index, path, paths);
index.pop_back();
path.pop_back();
}
}
}
};
实际上,观察某数是否已经访问过,不必使用一个vector,因为在vector中看一个数有没有访问过,需要o(n)的时间复杂度,此处完全可以用一个hashset来代替,看以下代码:
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int>> paths;
if (nums.empty()) {
return paths;
}
unordered_set<int> index;
vector<int> path;
permuteHelper(nums, index, path, paths);
return paths;
}
private:
void permuteHelper(const vector<int> &nums,
unordered_set<int> &index,
vector<int> &path,
vector<vector<int>> &paths) {
if (path.size() == nums.size()) {
paths.push_back(path);
return;
}
for (int ix = 0; ix < nums.size(); ix++) {
if (index.count(ix) == 0) {
index.insert(ix);
path.push_back(nums[ix]);
permuteHelper(nums, index, path, paths);
index.erase(ix);
path.pop_back();
}
}
}
};
能不能更进一步?这边完全可以使用一个数组来模拟hashset,来看代码:
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int>> paths;
if (nums.empty()) {
return paths;
}
bool *visited = new bool[nums.size()]();
vector<int> path;
permuteHelper(nums, visited, path, paths);
return paths;
}
private:
void permuteHelper(const vector<int> &nums,
bool *visited,
vector<int> &path,
vector<vector<int>> &paths) {
if (path.size() == nums.size()) {
paths.push_back(path);
return;
}
for (int ix = 0; ix < nums.size(); ix++) {
if (visited[ix] == false) {
visited[ix] = true;
path.push_back(nums[ix]);
permuteHelper(nums, visited, path, paths);
visited[ix] = false;
path.pop_back();
}
}
}
};
[LintCode] Permutations的更多相关文章
- [LintCode] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [算法专题] 深度优先搜索&回溯剪枝
1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...
- lintcode 中等题:permutations II 重复数据的全排列
题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Linux设置桌面图标 (双击运行jar包)
Ubuntu平台 预备条件: 1)平台是Gridion上的Ubuntu 2)安装了JRE (版本如下) 3)在IDE(我用的是IDEA)打包成可运行的jar文件 设置步骤: 1)新建.desktop文 ...
- [转]11种常见sqlmap使用方法详解
sqlmap也是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,只要你用的熟,秒杀各种工具,只是一个便捷性问题,sql注入另一方面就是手工党了,这个就另当别论了.今天把我一直 ...
- 效率类APP原型制作分享----Timeglass
本原型由国产Mockplus(原型工具)和iDoc(智能标注,一键切图工具)提供. 主要页面:启动页面.主页.添加事件页面.设置页面等. mp文件下载:点击这里 在线预览:http://run.moc ...
- java Mather 的 group 含义
参考博文: http://blog.csdn.net/java2king/article/details/4395067 明白了group 的 含义 public class Test { pub ...
- HYSBZ - 3676
模板题.问你一个串里最大的值(回文子串*出现次数) /* gyt Live up to every day */ #include<cstdio> #include<cmath> ...
- Django框架之Ajax和form组件
一.Django框架之查漏补缺 1)models,字段概况 name = models.CharField(max_length=) age = models.IntegerField() price ...
- 使用unidac 在linux 上无驱动直接访问MS SQL SERVER
随着delphi 10.2 开始了对Linux 的重新支持.devart 也迅速的发布了unidac 7.0, 最大的特性就是支持linux和MongoDB. 并有了其他更新: In this rel ...
- 46.UISearchBar的placeholder字体颜色和背景颜色
1.改变searchbar的searchField属性 UITextField *searchField = [searchbar valueForKey:@"searchField&quo ...
- fabric 安装
fabric 是一个python的库,fabric可以通过ssh批量管理服务器. 第一步安装依赖包 安装fabric依赖及pip yum install -y python-pip gcc pytho ...
- oracle创建表空间、添加数据库文件
创建表空间: create [undo|TEMPORARY]tablespace venn datafile '/opt/oracle/db01/app/oracle/oradata/OSSORCL/ ...