257.LeetCode | 49. 字母异位词分组
每天一个开发小知识
01
给定一个字符串数组,将字母异位词组合在一起
字母异位词指字母相同,但排列不同的字符串
示例:
输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
输出:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
map<string, vector<string>> m;
for (int i = 0; i < strs.size(); ++i)
{
string s = strs[i];
sort(s.begin(), s.end());
map<string, vector<string>>::iterator iter = m.find(s);
if (m.end() == iter)
{
vector<string> vec;
iter = m.insert(make_pair(s, vec)).first;
}
vector<string> * v = &(iter->second);
v->push_back(strs[i]);
}
vector<vector<string>> ret;
for (auto iter : m)
{
ret.push_back(iter.second);
}
return ret;
}
};