题目:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> res;
unordered_map<string, int> table;
for (int i = 0; i < strs.size(); i++) {
string s = strs[i];
sort(s.begin(), s.end());
//没出现过,加入
if (table.find(s) == table.end())
table[s] = i;
else {
if (table[s] != -1) {
res.push_back(strs[table[s]]);
table[s] = -1;
}
res.push_back(strs[i]);
}
}
return res;
}
};
解决字符串数组中所有异位词分组问题
本文提供了一种方法来处理字符串数组,找出并返回所有异位词分组。通过使用哈希表和排序技术,该算法有效地实现了这一目标。详细解释了实现步骤,包括初始化哈希表、遍历字符串数组、排序和查找异位词分组的过程。
385

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



