LetCode 49. 字母异位词分组

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    vector> groupAnagrams(vector& strs) {
        unordered_map> m;
        vector> v;
        for (int i = 0; i < strs.size(); i++){
            string str = strs[i];
            sort(str.begin(), str.end());
            if (!m.count(str)){
                vector v;
                v.push_back(strs[i]);
                m.insert(pair>(str, v));
            }
            else
                m[str].push_back(strs[i]);
        }
        vector> v;
        for (unordered_map>::iterator it = m.begin(); it != m.end(); it++)
            v.push_back(it->second);
        return v;
    }
};

你可能感兴趣的:(LetCode)