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列表输入到结果中去即可。

原文地址:https://www.cnblogs.com/forPrometheus-jun/p/11226576.html