您的位置:首页 > 其它

Leetcode 49 Group Anagrams

2016-09-12 12:49 344 查看
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: All inputs will be in lower-case.
将组成字母相同的单词归为一类。

做个简单映射就好了
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
unordered_map<string,int> mp;
int cnt=0;
for(int i=0;i<strs.size();i++)
{
string temp=strs[i];
sort(temp.begin(),temp.end());
if(mp.find(temp)==mp.end())
{
vector<string> s;
s.push_back(strs[i]);
result.push_back(s);
mp[temp]=cnt++;
}
else
result[mp[temp]].push_back(strs[i]);
}
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: