[LintCode] Permutations II
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的更多相关文章
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- Permutations II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...
随机推荐
- django模板语言和过滤
一:模板组成 HTML代码 + 逻辑控制代码 逻辑代码组成格式: 使用大括号来引用变量, {{ var_name }} 二: Template和Context对象 //进入该的django的项目环境 ...
- 数组转xml格式/xml格式转数组
数组转xml格式 $arr=array( 'username'=>'huahua', 'password'=>'123456', 'number'=>'15889652911', ) ...
- Spring 是如何解析泛型 - ResolvalbeType
Spring 是如何解析泛型 - ResolvalbeType Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Java ...
- Max Chunks To Make Sorted LT769
Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into som ...
- 解析vue2.0中render:h=>h(App)的具体意思
render:h=>h(App)是ES6中的箭头函数写法,等价于render:function(h){return h(App);}. 注意点:1.箭头函数中的this是 指向 包裹this所在 ...
- centos7构建python2.7常用开发环境
把下面的代码保存到一个sh文件中执行即可 yum -y install epel-release yum -y install python-pip yum -y install mysql-deve ...
- windows 下设置nginx负载均衡
#user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...
- django 静态文件
django中的静态文件,如图片,css样式jquery等等 在url最下面加上 from django.conf.urls.static import staticfrom django.conf ...
- java struts2 的 文件下载
jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...
- kbmMW均衡负载与容灾(3)(转载红鱼儿)
在kbmMW均衡负载与容灾(1)中,介绍了利用ClientTransport的OnReconnect事件,对联接的应用服务器的地址进行更换,做容灾处理.实际上,作者还给我们提供了另外一种机制,直接在C ...