字符串的排列
字符串的排列
http://www.nowcoder.com/questionTerminal/fe6b651b66ae47d7acce78ffdd9a96c7
class Solution {
public:
vector<string> Permutation(string str) {
if (str.size() == 0) {
return v;
}
func(str, 0);
sort(v.begin(), v.end());
return v;
}
void func(string &str, int begin){
if (begin == str.length()) {
s.insert(str);
if (s.size()!=before) {
v.push_back(str);
before = s.size();
}
return;
}
for (int i=begin; i<str.length(); i++) {
char t = str[i];
str[i] = str[begin];
str[begin] = t;
func(str, begin+1);
t = str[i];
str[i] = str[begin];
str[begin] = t;
}
}
private:
vector<string> v;
set<string> s;
int before = 0;
};