[LeetCode]Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
思考:map应用。遍历两次strs,第一次建立map,第二次打印符合提议的字符串。
class Solution {
private:
vector<string> res;
map<string,int> m;
public:
vector<string> anagrams(vector<string> &strs) {
int i;
for(i=0;i<strs.size();i++)
{
string temp=strs[i];
sort(temp.begin(),temp.end());
m[temp]++;
}
for(i=0;i<strs.size();i++)
{
string temp=strs[i];
sort(temp.begin(),temp.end());
map<string,int>::iterator iter=m.find(temp);
if(iter!=m.end()&&iter->second>1)
{
res.push_back(strs[i]);
}
}
return res;
}
};

浙公网安备 33010602011771号