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;
}
};