题目描述
Given an array of strings, group anagrams together.
样例
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
All inputs will be in lowercase.
The order of your output does not matter.
C++ 代码
//同一组的str根据map放到一个vector<string>去
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string,vector<string>> map;
for(auto str: strs){
string t=str;
sort(t.begin(),t.end());
map[t].push_back(str);
}
for(auto ga: map){
res.push_back(ga.second);
}
return res;
}
};