class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, int> hashTable;
vector<vector<string>> res;
int i = 0;
for(auto s : strs){
auto temp = s;
sort(temp.begin(), temp.end());
auto item = hashTable.find(temp);
if(item != hashTable.end()){
res[item->second].push_back(s);
}
else{
hashTable[temp] = i++;
res.resize(hashTable[temp] + 1);
res[hashTable[temp]].push_back(s);
}
}
return res;
}
};
LeetCode Hot100 49. 字母异位词分组
最新推荐文章于 2024-09-14 18:52:56 发布