[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 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...
随机推荐
- android udp 无法收到数据 (模拟器中)
解决方法:1. 运行模拟器2. 打开window 命令行执行:telnet localhost 55545554是模拟器的端口,执行之后会进入android console3. 在console下执行 ...
- vb中去掉string数组的一部分
今天碰到一个问题,登陆的时候,如果不需要验证手机号为空,则不去验证手机号 因为登陆的时候所有的验证信息都存放在一个数组里 Dim CheckUserInfo() As String ={UserBir ...
- oracle的常用99条语句
1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...
- CRC-32的原理和实现
/* crc32.c -- compute the CRC-32 of a data stream * Copyright (C) 1995-2002 Mark Adler * For conditi ...
- Globalization and accessibility for tile and toast notifications (Windows Store apps)
http://msdn.microsoft.com/en-us/library/windows/apps/hh831183.aspx 做 HighContrast时,采用以下分目录方式: /Proje ...
- [js]获取网页屏幕可见区域高度
document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...
- 矩阵快速幂小结-Hdu2604
矩阵快速幂可以想象为线性代数的矩阵相乘,主要是运用于高效的计算矩阵高次方. 将矩阵两两分组,若要求a^n,即知道a^(n/2)次方即可,矩阵快速幂便是运用的这个思路. 比方想求(A)^7那么(A)^6 ...
- 鲍姆-韦尔奇算法求解HMM参数
1. HMM模型参数求解概述 HMM模型参数求解根据已知的条件可以分为两种情况. 第一种情况较为简单,就是我们已知DD个长度为TT的观测序列和对应的隐藏状态序列,即{(O1,I1),(O2,I2),. ...
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
- c++中typedef、define、const、inline之间的区别
1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...