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的更多相关文章

  1. [LintCode] Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [算法专题] 深度优先搜索&回溯剪枝

    1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...

  3. lintcode 中等题:permutations II 重复数据的全排列

    题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...

  4. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  7. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  8. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  9. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. the example of dlsym

    void *handle; int i, (*fptr)(int); /* open the needed object */ handle = dlopen("/usr/home/me/l ...

  2. b2c项目访问

    http://xmpw.testbase.smi170.com:8091/member/movie_coupon_new.php

  3. js、css、img等浏览器缓存问题的2种解决方案

    转:http://www.jb51.net/article/42339.htm 浏览器缓存的意义在于提高了执行效率,但是也随之而来带来了一些问题,导致服务端修改了js.css,客户端不能更新,下面有几 ...

  4. 用 Python 写 Robot Framework 测试

    Robot Framework 框架是基于 Python 语言开发的,所以,它本质上是 Python 的一个库. 1.你懂 Python 语言. 2.又想使用 Robot Framework 测试框架 ...

  5. tomcat与jmeter

    jmeter无法提取出Tomcat之外的其他服务器的指标. 为了克服这一现状,研发了一个服务器代理,jmeter通过这个代理来获取性能数据. 代理使用的是sigar开源库,他是一个java通过部分和一 ...

  6. android 使用UDP发送数据 DatagramSocket 创建对象为null

    DatagramSocket socket=null; try { socket = new DatagramSocket();  //这里创建对象为空 } catch (SocketExceptio ...

  7. sqlserver 添加服务器链接 跨服务器访问数据库

    转载地址1:https://www.cnblogs.com/wanshutao/p/4137994.html //创建服务器链接 转载地址2:https://www.cnblogs.com/xulel ...

  8. socket.io的connect连接时不断的进行自动连接,并产生错误net::ERR_EMPTY_RESPONSE

    socket = io.connect('http://192.168.1.200:9043?uuid=333'); 执行上面的语句时,产生下面的错误: 后来经过排查,是由于项目的jdk版本过低引起的 ...

  9. Proxmox Reset Root Password

    http://c-nergy.be/blog/?p=1777 Step 1 – Boot your Proxmox VE machine. In the boot menu screen, you s ...

  10. 经典矩阵快速幂之二-----hdu2157(走k步到

    题意:(中问题,题意很简单 思路:a走k步到b,其实就是A^k,ans.mat[a][b]就是答案. 其实就是离散的邻接矩阵那个P(不想证明,逃 #include<cstdio> #inc ...