Loading

49. 字母异位词分组

题目

 

代码

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        unordered_map<string,vector<string>> table;
        for(auto i:strs)
        {
          auto temp=i;
          sort(temp.begin(),temp.end());
          table[temp].push_back(i);
        }
      
        for(auto i:table)
        {
          res.push_back(i.second);
        }
        return res;
    }
};

 

思路

利用map存储,key为排序后的字符串,value为排序前的字符串,因此所有排序后相同的字符串都属于一个分组。

posted @ 2018-09-17 21:11  李正浩  阅读(77)  评论(0编辑  收藏  举报