LeetCode 49 - Group Anagrams
来源:互联网 发布:淘宝主图1920显示不全 编辑:程序博客网 时间:2024/05/17 07:45
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
0 0
- Leetcode#49||Group Anagrams
- leetcode 49:Group Anagrams
- Leetcode 49: Group Anagrams
- 【leetcode】【49】Group Anagrams
- LeetCode 49 - Group Anagrams
- LeetCode 49 Group Anagrams
- leetcode(49):Group Anagrams
- Leetcode 49 Group Anagrams
- Leetcode-49Group Anagrams
- [Leetcode] #49 Group Anagrams
- LeetCode 49 Group Anagrams
- LeetCode-49:Group Anagrams
- Leetcode 49 Group Anagrams
- leetCode 49 Group Anagrams
- Leetcode 49 Group Anagrams
- LeetCode.49 Group Anagrams
- LeetCode 49 Group Anagrams
- Leetcode 49 Group Anagrams
- hdu4605 树状数组+离散化+dfs
- 显式调用构造函数
- 程序员每天自我洗脑和催眠
- C# 线程池中取消线程的三种方式
- 元素计算
- LeetCode 49 - Group Anagrams
- 《Java多线程编程核心技术》推荐
- hdu2612 Find a way(2次bfs)
- HDU Fire Net
- STL源码学习——空间配置器
- ACM_模板_中国剩余定理(互质与非互质)
- LINQ的预备知识
- uva10905连接n个数字,使得连成的数字最大
- 递归移位堆