LeetCode 49 - Group Anagrams
2016-03-04 21:36
645 查看
Group Anagrams
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:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
My Code
class Solution {public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
map<vector<int>, vector<string> > m;
map<vector<int>, vector<string> >::iterator it;
for (int i = 0; i < strs.size(); i++)
{
vector<int> cnts(26, 0);
for (int j = 0; j < strs[i].size(); j++)
{
cnts[strs[i][j] - 'a']++;
}
it = m.find(cnts);
if (it != m.end())
it->second.push_back(strs[i]);
else
m.insert(pair<vector<int>, vector<string> >(cnts, vector<string>(strs.begin()+i, strs.begin()+i+1)));
}
vector<vector<string> > groups;
for (it = m.begin(); it != m.end(); it++)
{
sort(it->second.begin(), it->second.end());
groups.push_back(it->second);
}
return groups;
}
};Runtime: 168 ms
相关文章推荐
- LINQ的预备知识
- HDU Fire Net
- eclipse内存溢出报错:java.lang.OutOfMemoryError:Java heap space
- 《Java多线程编程核心技术》推荐
- 《Java多线程编程核心技术》推荐
- 《Java多线程编程核心技术》推荐
- 《Java多线程编程核心技术》推荐
- 元素计算
- 第一次作业。
- Java自定义异常类
- hdu4605 树状数组+离散化+dfs
- hdu4605 树状数组+离散化+dfs
- 终于转正了
- 06.3.4工作笔记
- 白话Spring(基础篇)---AOP(实例篇-1)
- distributor之Interrupt Set/Clear-Active Registers, GICD_IS/CACTIVERn
- PHP中 post 与get的区别 详细说明
- 大三下学期计划
- C/C++的内存处理函数
- Mac OS Locate 查找文件指令