[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 ...
随机推荐
- flask学习视频
https://study.163.com/course/courseMain.htm?courseId=1004091002 主要 https://www.cnblogs.com/senlinyan ...
- debian9安装mysql mariadb
debian9下mysql 替换成mariadb-server-10.1 不过两者类似 具体可见 <MySQL和mariadb区别> http://ask.chinaunix.net/qu ...
- canvas 实现圆环效果
var race = document.getElementById('race'); var cxt = race.getContext('2d'); var ang = 0; var speed ...
- Django高级篇一RESTful架构及API设计
一.什么是RESTful架构? 通过互联网通信,建立在分布式体系上"客户端/服务器模式”的互联网软件,具有高并发和高延时的特点. 简单的来说,就是用开发软件的模式开发网站.网站开发,完全可以 ...
- UGUI控制UI的显示层级
1.调用transform.SetAsLastSibling();将该UI的显示层级调到最上面. 调用transform.SetAsFirstSibling();将该UI的显示层级调到最下面. 2. ...
- Django的学习(二)————Templates
一.django的模板: 在settings.py的文件中可以看到并设置这个模板. 1.直接映射: 通过建立的文件夹(templates)和文件(html)来映射. <!DOCTYPE html ...
- Spring mvc项目,使用jetty插件和tomcat路径相差一个项目名
pom.xml: jetty 插件配置: <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId& ...
- nginx 502 Bad Gateway 错误解决办法
nginx出现502有很多原因,但大部分原因可以归结为资源数量不够用,也就是说后端PHP-fpm处理有问题,nginx将正确的客户端请求发给了后端的php-fpm进程,但是因为php-fpm进程的问题 ...
- 2018.12.23 bzoj2865&&1396: 字符串识别(后缀自动机+线段树)
传送门 卡空间差评! 题意简述:给一个字串,对于每个位置求出经过这个位置且只在字串中出现一次的子串的长度的最小值. 解法:先建出samsamsam,显然只有当sizep=1size_p=1sizep ...
- Jquery中parentsUntil函数调用最容易犯的三个错误
来自 :http://jquery01.diandian.com/post/2012-01-16/14500044 Jquery中parentsUntil函数调用最容易犯的三个错误 Jquery的pa ...