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

Group Anagrams

时间:2015-11-08 20:45:40      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

Group Anagrams

My Submissions
Total Accepted: 54426 Total Submissions: 217518 Difficulty: Medium

 

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
Return:

[
  ["ate", "eat","tea"],
  ["nat","tan"],
  ["bat"]
]

 

Note:

  1. For the return value, each inner list‘s elements must follow the lexicographic order.
  2. All inputs will be in lower-case.

 

 

Subscribe to see which companies asked this question

 

 

 

觉得还是应该锻炼一下代码能力,这几天一直看课件,找知识,动手能力确实下降。

 1 class Solution {
 2 public:
 3     vector<vector<string>> groupAnagrams(vector<string>& strs) {
 4         vector<vector<string>> result;
 5         std:sort(strs.begin(),strs.end());
 6         map<string,vector<string>> strtoarray;
 7         int i;
 8         for(i=0;i<strs.size();i++){
 9             strtoarray[tostr(strs[i])].push_back(strs[i]);
10         }
11         map<string,vector<string>>::iterator it;
12         for(it=strtoarray.begin();it!=strtoarray.end();it++){
13             result.push_back(it->second);
14         }
15         return result;
16     }
17     
18     
19     string tostr(string str){
20         std:sort(str.begin(),str.end());
21         return str;
22     }
23 };

 

Group Anagrams

标签:

原文地址:http://www.cnblogs.com/Deribs4/p/4948005.html

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