十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
题目链接:https://leetcode.cn/problems/group-anagrams/
思路如下:
输入:strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
对每个字符串进行排序:strs = [“aet”, “aet”, “ant”, “aet”, “ant”, “abt”]
如果排序后的字符串相同,则将它们归为同一组:[[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
C++代码如下:
class Solution {public:
vector>groupAnagrams(vector& strs) {unordered_map>str2vec;
for (auto& str : strs) {auto tmp = str;
sort(tmp.begin(), tmp.end());
str2vec[tmp].push_back(str);
}
vector>res;
for (auto& item : str2vec) {res.push_back(item.second);
}
return res;
}
};
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧