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

For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].

class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> paths;
if (nums.empty()) {
return paths;
} vector<int> index;
vector<int> path;
permuteUniqueHelper(nums, index, path, paths);
return paths; } private:
void permuteUniqueHelper(const vector<int> &nums,
vector<int> &index,
vector<int> &path,
vector<vector<int>> &paths) {
if (path.size() == nums.size()) {
paths.push_back(path);
return;
} // 保证相同的数不在同一位置出现两次以上
unordered_set<int> hashset;
for (int ix = 0; ix < nums.size(); ix++) {
if (find(index.begin(), index.end(), ix) == index.end() && hashset.count(nums[ix]) == 0) {
hashset.insert(nums[ix]); index.push_back(ix);
path.push_back(nums[ix]);
permuteUniqueHelper(nums, index, path, paths);
index.pop_back();
path.pop_back();
}
}
}
};

能否对空间复杂度做进一步的优化?

class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of unique permutations.
*/
vector<vector<int> > permuteUnique(vector<int> &nums) {
// write your code here
vector<vector<int>> paths;
if (nums.empty()) {
return paths;
} sort(nums.begin(), nums.end());
bool *visited = new bool[nums.size()]();
vector<int> path;
permuteUniqueHelper(nums, visited, path, paths);
return paths;
} private:
void permuteUniqueHelper(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] == true || ix > 0 && nums[ix - 1] == nums[ix] && visited[ix - 1] == false) {
continue;
} visited[ix] = true;
path.push_back(nums[ix]);
permuteUniqueHelper(nums, visited, path, paths);
visited[ix] = false;
path.pop_back();
}
}
};

[LintCode] Permutations II的更多相关文章

  1. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  2. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  3. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  4. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  5. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  6. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  7. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...

  8. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  9. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

随机推荐

  1. ASP.NET 在OnClientClick中js方法直接调用Eval绑定字段的数据

    最近有一项目中使用到了asp.net的GridView控件.需要在前端被点击某一行数据时,前端获取到改行后台绑定的数据序列号.遍用<%# Bind("ID) %>.<%# ...

  2. db2 解锁表

    db2 set integrity for ACT_RU_VARIABLE immediate checked

  3. @Valid报错 No validator could be found for constraint

    使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...

  4. 关于Windows 8 合约

    浅谈win8合约: http://www.devdiv.com/Windows_Metro-windows_metro_app_windows_contracts_-thread-131717-1-1 ...

  5. Java在dos界面运行java源文件编译成功,但运行虚拟机时出现错误:“找不到或无法加载主类”的问题

    (一)首先检查环境变量配置有没有问题, 1PATH为%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 2CLASSSPATH为.;%JAVA_HOME%\lib\dt.jar; ...

  6. 【linux轻松学】修改文件权限

    用chmod修改文件权限,此命令非常重要. 用户范围:u 表示当前用户g 表示当前群组o 除u,g之外的用户和群组a 所有用户和群组 权限代号:r :读,用数字4表示w :写,用数字2表示x :执行, ...

  7. GUI的最终选择Tkinter模块练习篇

    一.Canvas画布练习 1)简单的绘制图框 from tkinter import * # 构建一个窗口 tk = Tk() # 画布 canvas= Canvas(tk,width=,height ...

  8. css固宽截取字符串

    width:200px; white-space:nowrap; word-break:keep-all; overflow:hidden; text-overflow:ellipsis; 移动端支持 ...

  9. ThinkPHP getBy动态查询

    getBy动态查询 ThinkPHP getBy动态查询是一个魔术方法,可以根据某个字段名称动态得到对应的一条数据记录. 根据用户名(username)查询对应的用户资料记录: public func ...

  10. react native的注释

    在react native 中是这样写注释的: {/*这里是注释*/}