现在的位置: 首页 > 综合 > 正文

LeetCode–Anagrams

2018年10月01日 ⁄ 综合 ⁄ 共 387字 ⁄ 字号 评论关闭
class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        vector<string> res;
        map<string, vector<string>> strmap;
        
        int size = strs.size();
        for(auto it=strs.begin(); it!=strs.end(); it++)
        {
            string s = *it;
            sort(s.begin(), s.end());
            strmap[s].push_back(*it);
        }
        
        for(auto it=strmap.begin(); it!=strmap.end(); it++)
        {
            if(it->second.size() > 1)
                res.insert(res.end(), it->second.begin(), it->second.end());
        }
        return res;
    }
};

抱歉!评论已关闭.