LetCode 49. 字母异位词分组

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

猜你喜欢

转载自blog.csdn.net/wbb1997/article/details/80988468
今日推荐