LeetCode 49. Group Anagrams

题目

签到题

c++

class Solution {
public:
    map<string,int> m;
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        
        vector<string> strs2 = strs;
        for(int i=0;i<strs.size();i++)
        {
            sort(strs[i].begin(),strs[i].end());
        }
        
        vector<vector<string>> res;
        int pos=0;
        vector<string> ans;
        for(int i=0;i<strs.size();i++)
        {
            if(m[strs[i]]==0)
            {
                m[strs[i]]=++pos;
                ans.clear();
                ans.push_back(strs2[i]);
                
                res.push_back(ans);
            }
            else
            {
                res[m[strs[i]]-1].push_back(strs2[i]);
            }
        }
        return res;
    }
};
posted @ 2019-08-26 08:10  Shendu.CC  阅读(82)  评论(0编辑  收藏  举报