LeetCode: Anagrams

1264 단어 functioniterator
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
구수 그룹의 모든anagrams
class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> result(0);
        int nSize = strs.size();
        if (nSize == 0) return result;
        
        vector<string> tmp(strs);
        for(int i = 0; i < nSize; ++i)
        {
            sort(tmp[i].begin(), tmp[i].end());
        }
        
        map<string, vector<int> > ana;
        for (int i = 0; i < nSize; ++i)
        {
            ana[tmp[i]].push_back(i);
        }
        
        multimap<string, vector<int> >::iterator itr = ana.begin();
        while(itr != ana.end())
        {
            if (itr->second.size() > 1)
            {
                while(itr->second.size() > 0)
                {
                    result.push_back(strs[itr->second.back()]);
                    itr->second.pop_back();
                }
            }
            ++itr;
        }
        return result;
    }
};

좋은 웹페이지 즐겨찾기