题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
class Solution { public: vector<string> Permutation(string str) { vector<string> ans; backtracking(str, 0, ans); //去重复 unordered_set<string> ansset; vector<string> newans; for(int i = 0; i < ans.size(); i++){ ansset.insert(ans[i]); } for(auto pos = ansset.begin(); pos != ansset.end(); pos++){ newans.push_back(*pos); } return newans; } void backtracking(string& str, int level, vector<string>& ans){ if(level==str.length()-1){ ans.push_back(str); return; } for(int i = level; i < str.length(); i++){ swap(str[i],str[level]); backtracking(str, level+1, ans); swap(str[i],str[level]); } } };