您的位置:首页 > 其它

49. Group Anagrams

2016-05-14 17:32 295 查看
Given an array of strings, group anagrams together.

For example, given: 
["eat", "tea", "tan", "ate", "nat", "bat"]


Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]


Note:

For the return value, each inner list's elements must follow the lexicographic order.

All inputs will be in lower-case.

这里主要是map和set的运用。

代码:
class Solution {
public:
vector<vector<string> > groupAnagrams(vector<string>& strs)
{
int n=strs.size();
vector<vector<string> >res;
unordered_map<string,multiset<string> >m;
for(int i=0;i<n;i++)
{
string tmp=strs[i];
sort(tmp.begin(),tmp.end());
m[tmp].insert(strs[i]);
}
unordered_map<string,multiset<string> >::iterator it;
for(it=m.begin();it!=m.end();it++)
{
vector<string>vec((*it).second.begin(),(*it).second.end());
res.push_back(vec);
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  set map