【算法】【字符串】Leetcode哈希表相關高頻面試題

字母異位詞分組

題目鏈接:https://leetcode-cn.com/problems/group-anagrams/

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, int> hashmap;
        vector<vector<string>> res;
        for(auto str : strs)
        {
            auto tmp = str;
            sort(tmp.begin(), tmp.end());
            if(hashmap.count(tmp))
            {
                res[hashmap[tmp]].push_back(str);
            }
            else
            {
                hashmap[tmp] = res.size();
                res.push_back({str});
            }
        }
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章