[leetcode 47] Permutations II
2015-01-11 11:22
555 查看
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
and
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> > res; int N; vector<vector<int> > permuteUnique(vector<int> &num) { N = num.size(); res.clear(); sort(num.begin(),num.end()); perm(num,0); return res; } bool isSwap(vector<int>& num, int s, int e) { int i = s; while (num[i] != num[e] && i < e) i++; if (i == e) return true; else return false; } void perm(vector<int> &num, int i) { if (i == N-1) { res.push_back(num); return ; } for (int j = i; j < N; j++) { if (!isSwap(num, i, j)) continue; swap(num[i],num[j]); perm(num,i+1); swap(num[i],num[j]); } } };
相关文章推荐
- LeetCode47——Permutations II
- leetcode 47:Permutations II
- leetcode || 47、 Permutations II
- leetcode 47. Permutations II
- leetcode[47]Permutations II
- Leetcode:47. Permutations II
- Leetcode 47 Permutations II
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
- LeetCode 47. Permutations II
- LeetCode_47---Permutations II
- 个人记录-LeetCode 47. Permutations II
- leetcode编程记录19 #47 Permutations II
- LeetCode 43.Multiply Strings & 46.Permutations & 47.Permutations II
- leetcode - 47. Permutations II(全排列)
- LeetCode 47 Permutations II
- [leetcode] 47. Permutations II
- Leetcode 47 Permutations II
- Leetcode 47. Permutations II
- 【LeetCode】47. Permutations II
- LeetCode 47. Permutations II(生成不同的组合-dfs)