LeetCode_49. 字母异位词分组

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/all_about_WZY/article/details/102704119

题目描述:

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        unordered_map<string,vector<string>> mp;
        for(string s:strs){
            string t=s;
            sort(t.begin(),t.end());//给出起点和终点,可以直接对字符串(原地)进行按字母表顺序排序
            if(mp.find(t)==mp.end()){
                vector<string> temp;
                temp.push_back(s);
                mp[t]=temp;
            }
            else
                mp[t].push_back(s);
        }
        unordered_map<string,vector<string>>::iterator it=mp.begin();
        for(;it!=mp.end();it++){
            res.push_back(it->second);//map容器 it->first访问key,it->second访问value
        }
        return res;
    }
};

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/all_about_WZY/article/details/102704119