力扣题-11.30
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:49. 字母异位词分组
解题思想:将单词进行排序之后通过哈希表进行返回
class Solution(object):def groupAnagrams(self, strs):""":type strs: List[str]:rtype: List[List[str]]"""mp = collections.defaultdict()for st in strs:key = "".join(sorted(st))if key in mp:mp[key].append(st)else:mp[key] = [st]return list(mp.values())
class Solution {
public:vector<vector<string>> groupAnagrams(vector<string>& strs) {std::unordered_map<std::string, std::vector<std::string>> mp;for (const std::string& st : strs) {std::string key = st;std::sort(key.begin(), key.end());if (mp.find(key) != mp.end()) {mp[key].push_back(st);} else {mp[key] = {st};}}std::vector<std::vector<std::string>> result;for (const auto& entry : mp) {result.push_back(entry.second);}return result;}
};