Skip to content

Commit c2ae3fa

Browse files
authored
Create str.cpp
1 parent 6f3f960 commit c2ae3fa

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

0____剑指offer/other/38/str.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution
2+
{
3+
public:
4+
void permutation(string& str, string& temp,vector<string>& strs)
5+
{
6+
auto index = temp.size();
7+
if (index == str.size())
8+
{
9+
strs.push_back(temp);
10+
return;
11+
}
12+
std::set<int> cache;
13+
for (int i = index;i <str.size(); i++)
14+
{
15+
auto ch = str[i];
16+
if (cache.find(ch) == cache.end())
17+
{
18+
cache.insert(ch);
19+
temp.push_back(ch);
20+
str[i] = str[index];
21+
str[index] = ch;
22+
permutation(str, temp, strs);
23+
str[index] = str[i];
24+
str[i] = ch;
25+
temp.pop_back();
26+
}
27+
}
28+
}
29+
vector<string> permutation(string s)
30+
{
31+
vector<string> rst;
32+
string temp;
33+
temp.resize(s.size());
34+
temp.clear();
35+
permutation(s, temp, rst);
36+
return rst;
37+
}
38+
};

0 commit comments

Comments
 (0)