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. How to Set Ckeditor ReadOnly Mode

    CKEditor API makes it possible to render the editor content read-only (and thus impossible for the u ...

  2. xcode如何运行下载的demo工程

    1. 首先你需要改Bundle Identifier,修改成别人没注册过的. 2. 选中 “Automatically manage signing” 3. 在Team里面添加自己的个人帐号了.

  3. openssl初步使用

    centos平台 md5.c #include <stdio.h> #include <string.h> #include <stdlib.h> //#inclu ...

  4. 清华大学 TUNA 协会

    https://tuna.moe/ 技术,实力,优越感,环境..,镜像

  5. Controller异步模式

    转载: https://blog.csdn.net/yingxiake/article/details/51193319 因为服务器请求处理线程的总数是有限的,如果类似的请求多了,所有的处理线程处于阻 ...

  6. kbmmw 中XML 操作入门

    delphi 很早以前就自带了xml 的操作,最新版里面有三种XML 解释器,一种是MSXML,看名字就知道 这个是微软自带的,这个据delphi 官方称是速度是最快的,但是只能在windows 上使 ...

  7. java web 大总结

    C/s架构:        socket.serversocket.awt/swing做一个客户端软件        建好socket连接后,通过IO流交换数据.数据格式由各个开发者自己确定,B/C架 ...

  8. 10个相见恨晚的 Java 在线练手项目

    10个有意思的Java练手项目: 1.Java 开发简单的计算器 难度为一般,适合具有 Java 基础和 Swing 组件编程知识的用户学习 2.制作一个自己的 Java 编辑器 难度中等,适合 Ja ...

  9. iOS知识基础篇 static

    static关键字的作用  一.隐藏 通过static修饰的函数或者变量,在该文件中,所有位于这条语句之后的函数都可以访问,而其他文件中的方法和函数则不行: 二.静态变量 类方法不可以访问实例变量(函 ...

  10. 2018.11.05 NOIP模拟 列队(差分约束)

    传送门 直接建边跑差分约束就可以了. 代码