码迷,mamicode.com
首页 > 其他好文 > 详细

49. Group Anagrams

时间:2018-10-18 01:14:09      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:put   str   group   lin   info   NPU   code   push   map   

 

Given an array of strings, group anagrams together.

Example:

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

Note:

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

AC code:

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> mp;
        vector<vector<string>> res;
        for (auto str : strs) {
            string tage = str;
            sort(tage.begin(), tage.end());
            mp[tage].push_back(str);
        }
        for (auto m : mp) {
            sort(m.second.begin(), m.second.end());
            res.push_back(m.second);
        }
        return res;
    }
};
Runtime: 36 ms, faster than 37.09% of C++ online submissions for Group Anagrams.

 

 

49. Group Anagrams

标签:put   str   group   lin   info   NPU   code   push   map   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/9807315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!