【LeetCode】49. 字母异位词分组

题目

添加链接描述

思路

遍历字符串数组 strs。对第一个字符串 "eat"执行: 将 “eat” 转换为字符数组 [‘e’, ‘a’, ‘t’] 对字符数组进行排序,得到 [‘a’, ‘e’, ‘t’] 使用排序后的字符数组创建 key “aet” 从 map 中获取 key 为 “aet” 的值,由于不存在,因此创建一个新的空列表 list = [] 将 “eat” 添加到 list 中,现在 list = [“eat”] 将 key 为 “aet”,value 为 [“eat”] 的键值对存入 map 对第二个字符串 “tea” 执行类似操作: 字符数组为 [‘t’, ‘e’, ‘a’],排序后为 [‘a’, ‘e’, ‘t’],key 为 “aet” 从 map 中获取 key 为 “aet” 的值,存在,为 [“eat”] 将 “tea” 添加到列表中,现在列表为 [“eat”, “tea”] 将更新后的列表存入 map,key 为 “aet” 对其余字符串 “tan”, “ate”, “nat”, “bat” 执行类似操作,最终 map 为: key 为 “aet”,value 为 [“eat”, “tea”, “ate”] key 为 “ant”,value 为 [“tan”, “nat”] key 为 “abt”,value 为 [“bat”] 从 map 中获取所有 value,构造结果列表,即 [ [“eat”, “tea”, “ate”], [“tan”, “nat”], [“bat”] ] 可以看到,通过将每个字符串排序作为 key,并存储字母异位词的字符串列表作为 value。

代码

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>>mp;
        for(string& str: strs)
        {
            string key=str;
            sort(key.begin(),key.end());
            mp[key].emplace_back(str);
        }
        vector<vector<string>>ans;
        for(auto it=mp.begin();it!=mp.end();it++)
        {
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

你可能感兴趣的:(leetcode,算法)