LeetCode - Anagrams

发布时间:2017-2-24 9:27:33 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"LeetCode - Anagrams",主要涉及到LeetCode - Anagrams方面的内容,对于LeetCode - Anagrams感兴趣的同学可以参考一下。

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. class Solution { public: vector<string> anagrams(vector<string> &strs) { vector<string> result; if(strs.empty()){ return result; } string s; map<string,vector<int> > wordMap; map<string,vector<int> >::iterator iter; for(int i=0;i<strs.size();i++){ s=strs[i]; sort(s.begin(),s.end()); iter=wordMap.find(s); if(iter==wordMap.end()){ vector<int> vec; vec.push_back(i); wordMap.insert(make_pair(s,vec)); }else{ iter->second.push_back(i); } } iter=wordMap.begin(); while(iter!=wordMap.end()){ if(iter->second.size()>1){ for(int i=0;i<iter->second.size();i++){ result.push_back(strs[iter->second[i]]); } } iter++; } return result; } };

下一篇:hascode 的理解