class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
// 字母异位词的字符串排序后是相同的
unordered_map<string, vector<string>> hash;
for(auto str : strs)
{
auto tem = str;
sort(tem.begin(), tem.end());
hash[tem].push_back(str);
}
vector<vector<string>> res;
for(auto e : hash) res.push_back(e.second);
return res;
}
};
49.字母异位词分组
最新推荐文章于 2024-07-25 21:11:40 发布