1. 程式人生 > 其它 >【LeetCode】49. 字母異位詞分組【STL】

【LeetCode】49. 字母異位詞分組【STL】

技術標籤:# LeetCode

傳送門

49. 字母異位詞分組

思路

unordered的簡單使用

AC程式碼

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> mp;
        for(auto &str: strs){
            string key = str;
            sort
(key.begin(),key.end()); mp[key].push_back(str); } vector<vector<string> > ans; for(auto it = mp.begin(); it != mp.end(); it++){ ans.push_back(it->second); } return ans; } };