Created
May 9, 2014 02:15
-
-
Save HaiyangXu/f1700d8b28482d4a2725 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<vector<int> > result; | |
vector<vector<int> > permuteUnique(vector<int> &num) { | |
permute(num,0); | |
return result; | |
} | |
void permute(vector<int> &num,int index) | |
{ | |
unordered_set<int> umap; | |
if(index==num.size()) | |
result.push_back(num); | |
else | |
for(int i=index;i<num.size();++i) | |
{ | |
std::pair<unordered_set<int>::iterator,bool> it=umap.insert(num[i]); | |
if(it.second) | |
{ | |
swap(num[i],num[index]); | |
permute(num,index+1); | |
swap(num[i],num[index]); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用了一个hash set去重,如果当前待交换的项和之前相同,则不进行递归。