49. Group Anagrams

Posted forprometheus-jun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了49. Group Anagrams相关的知识,希望对你有一定的参考价值。

description:

Given an array of strings, group anagrams together
Note:
Note:

All inputs will be in lowercase.
The order of your output does not matter.

Example:

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

answer:

class Solution 
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) 
        vector<vector<string>> res;
        unordered_map<string, vector<string>> m;
        for (string str : strs) 
            string t = str;
            sort(t.begin(), t.end());
            m[t].push_back(str); //push_back
        
        for(auto a: m) 
            res.push_back(a.second); //取字典的value值
        
        return res;
    
;

relative point get√:

a.second()

hint :

每个在一个组里的列表对应字母表顺序重新排列后都是一样的,所以用重排后的字母作为key, 最后将所有value列表输入到结果中去即可。

以上是关于49. Group Anagrams的主要内容,如果未能解决你的问题,请参考以下文章

49. Group Anagrams

[leetcode][49] Group Anagrams

#Leetcode# 49. Group Anagrams

49. Group Anagrams(js)

49. Group Anagrams

49. Group Anagrams