卡码讲解
49

字母异位词分组

Solution.cpp
1class Solution {
2public:
3 vector<vector<string>> groupAnagrams(vector<string>& strs) {
4 vector<vector<string>> result;
5 unordered_map <string,vector<string> > umap;
6
7 for (int i = 0; i < strs.size(); i++) {
8 string tmp = strs[i];
9 sort(tmp.begin(), tmp.end());
10 umap[tmp].push_back(strs[i]);
11 }
12
13 for (auto& collection : umap) {
14 result.push_back(collection.second);
15 }
16 return result;
17 }
18};

当前步骤

初始化结果集和哈希表 (unordered_map)

Input Array (strs)

eat
tea
tan
ate
nat
bat

Hash Map (unordered_map)

等待数据...
进度0%
速度