49. Group Anagrams**
https://leetcode.com/problems/group-anagrams/
题目描述
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++ 实现 1
使用哈希表.
lass Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> record;
for (auto &s : strs) {
string t = s;
std::sort(t.begin(), t.end());
record[t].push_back(s);
}
for (auto &m : record) res.push_back(m.second);
return res;
}
};