原题题目
代码实现(首刷自解 太烧了这能AC)
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
multimap<string,string> m;
unordered_set<string> s;
int count = 0;
for(const auto& str:strs)
{
auto temp(str);
sort(temp.begin(),temp.end());
s.insert(temp);
m.insert(make_pair(temp,str));
}
vector<vector<string>> ret(s.size());
for(const auto& str:s)
{
auto left = m.lower_bound(str);
auto right = m.upper_bound(str);
while(left != right)
ret[count].push_back((left++)->second);
++count;
}
return ret;
}
};
代码实现(二刷自解 DAY 147 C++)
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> ret;
unordered_map<string,vector<string>> map;
for(const auto& str:strs)
{
auto temp(str);
sort(temp.begin(),temp.end());
map[temp].emplace_back(str);
}
for(const auto& v:map)
ret.emplace_back(v.second);
return ret;
}
};