LeetCode 49. Group Anagrams


https://leetcode.com/problems/anagrams/


拿来练一练multimap

思路很简单,每个word都sort  然后根据这个结果决定放哪个multimap的元素

学到multimap的遍历方法啊,有点麻烦


class Solution {
public:
    vector < vector<string> > groupAnagrams(vector<string>& strs) {
        multimap < string, string > mp;
        for(int i=0;i<strs.size();i++) {
            string tmp = strs[i];
            sort(tmp.begin(), tmp.end());
            mp.insert(make_pair(tmp, strs[i]));
        }
        vector< vector<string> > ret;
        for(multimap< string, string >::iterator itr=mp.begin(); itr != mp.end(); ) {
            vector <string> tmp;
            int count = mp.count((*itr).first );// (*itr).first
            for(int i=0;i<count;i++) {
                tmp.push_back( (*itr).second );
                itr++;
            }
            sort(tmp.begin(), tmp.end());
            ret.push_back(tmp);
        }
        return ret;
    }
};



你可能感兴趣的:(LeetCode 49. Group Anagrams)