建立一个哈希表,记录相同字符序列对应的字符串:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string> > table;
for(int i=0;i<strs.size();i++)
{
string temp=strs[i];
sort(temp.begin(),temp.end());
table[temp].push_back(strs[i]);
}
vector<vector<string>> res;
unordered_map<string,vector<string> >::iterator it=table.begin();
for(;it!=table.end();it++)
res.push_back(it->second);
return res;
}
本文介绍了一种使用哈希表来解决字符串中的同字母异序词分组问题的方法。通过将每个字符串排序后作为哈希表的键,原始字符串作为值存储,实现了快速查找和分组。
462

被折叠的 条评论
为什么被折叠?



