Commit 37d57186 authored by yaozc's avatar yaozc
Browse files

problem 49 solved

parent 0cb6a95d
......@@ -182,3 +182,34 @@ public:
stack<TreeNode*> mystack;
vector<int> result;
};
// 49. Group Anagrams
// 有相同字符的单词全进一个vector
// 本质其实是map的应用,难点在如果鉴别share相同字母的单词,这里用了sort后作为map key的办法
// well,虽然low,但work了 lol
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
unordered_map<string, vector<string>> umap;
for(auto &key: strs){
string sum = key;
sort(sum.begin(), sum.end());
auto iter = umap.find(sum);
if (iter == umap.end()){
vector<string> temp;
temp.push_back(key);
umap[sum] = temp;
}
else{
iter->second.push_back(key);
}
}
for (auto itr = umap.begin(); itr != umap.end(); itr++){
result.push_back(itr->second);
}
return result;
}
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment