2013.12.15 05:30
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]
.
Solution:
This time the array may contain duplicates, so I implemented the next_permutation() myself, and called it until all the permutations are traversed.
You can see the two problems here and find their connection with this problem: Next Permutation and Permutations.
Time complexity is O(n * n! / (a! * b! * c! * ...)), where a, b, c, ... are the number of groups of duplictate elements. Space complexity is O(n).
Accepted code:
1 // 1CE, 2MLE, 1AC, not so easy 2 #include <algorithm> 3 using namespace std; 4 5 class Solution { 6 public: 7 vector<vector<int> > permuteUnique(vector<int> &num) { 8 // IMPORTANT: Please reset any member data you declared, as 9 // the same Solution instance will be reused for each test case. 10 int i, j; 11 int n; 12 13 n = result.size(); 14 for(i = 0; i < n; ++i){ 15 result[i].clear(); 16 } 17 result.clear(); 18 19 sort(num.begin(), num.end()); 20 n = num.size(); 21 if(n <= 0){ 22 return result; 23 } 24 while(true){ 25 result.push_back(num); 26 for(i = n - 1; i > 0; --i){ 27 if(num[i - 1] < num[i]){ 28 break; 29 } 30 } 31 if(i <= 0){ 32 break; 33 } 34 --i; 35 for(j = n - 1; j > i; --j){ 36 if(num[j] > num[i]){ 37 break; 38 } 39 } 40 myswap(num[j], num[i]); 41 // 2MLE here, reverse(i + 1, n - 1), not reverse(i, n - 1); 42 reverse(num, i + 1, n - 1); 43 } 44 45 return result; 46 } 47 private: 48 vector<vector<int>> result; 49 void myswap(int &x, int &y) { 50 int tmp; 51 52 tmp = x; 53 x = y; 54 y = tmp; 55 } 56 57 void reverse(vector<int> &num, int left, int right) { 58 int i; 59 60 if(left > right){ 61 reverse(num, right, left); 62 return; 63 } 64 65 i = left; 66 // 1CE here, how did you submit before even finishing it!!! 67 while(i < left + right - i){ 68 myswap(num[i], num[left + right - i]); 69 ++i; 70 } 71 } 72 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)