LeetCode-Permutations II
来源:互联网 发布:商侃的淘宝店 编辑:程序博客网 时间:2024/05/18 02:33
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]
.
Code:
<span style="font-size:14px;">class Solution {public: bool valid(vector<int> &num, int begin, int end) { for (int i = begin; i < end; ++i) if (num[i] == num[end]) return false; return true; } void helper(vector<vector<int> > &results, vector<int> &num, const int &length, int index) { if (index == length) { results.push_back(num); return; } helper(results, num, length, index+1); for (int i = index+1; i < length; ++i) if (valid(num, index, i)) { swap(num[index], num[i]); helper(results, num, length, index+1); swap(num[index], num[i]); } } vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int> > results; const int length = num.size(); if (length == 0) return results; if (length == 1) { results.push_back(num); return results; } sort(num.begin(), num.end()); helper(results, num, length, 0); return results; }};</span>
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- LeetCode: Permutations II
- LeetCode Permutations II
- LeetCode: Permutations II
- [Leetcode] Permutations II
- [LeetCode] Permutations II
- [Leetcode] Permutations II
- leetcode Permutations II
- [LeetCode]Permutations II
- leetcode - Permutations II
- LeetCode-Permutations II
- [leetcode] Permutations II
- 今年的秋天来的太早
- c#创建使用Dll动态链接库
- 如何从键盘录入数据调用排序算法排序?
- hdu 4951 Multiplication table
- Apple Tree
- LeetCode-Permutations II
- dumpsys使用详解
- Zend Studio 开发Resource is out of sync with the file system的解决办法
- sql -复制已有表结构,创建新表
- TCP 长连接与短连接的区别与实现
- linux 路由表 的一些相关资料
- SuSELinux上部署pppoe-server服务,rp-pppoe
- 最大流Edmonds-Karp模板
- LeetCode OJ - Binary Tree Zigzag Level Order Traversal