49. 字母异位词分组
为保证权益,题目请参考 49. 字母异位词分组(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string> &strs)
{
map<string, vector<string>> maps;
for (string str : strs)
{
string str_tmp = str;
sort(str_tmp.begin(), str_tmp.end());
if (maps.find(str_tmp) != maps.end())
{
maps[str_tmp].push_back(str);
}
else
{
vector<string> vec_tmp;
vec_tmp.push_back(str);
maps.insert(map<string, vector<string>>::value_type(str_tmp, vec_tmp));
}
}
vector<vector<string>> res;
for (map<string, vector<string>>::iterator iter = maps.begin(); iter != maps.end(); iter++)
{
res.push_back(iter->second);
}
return res;
}
};
int main()
{
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42