首页 > > > [LeetCode] Group Anagrams
Group Anagrams
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.
解题思路:
用hash的方法,将所有的字符分组。
class Solution {
public:
vector> groupAnagrams(vector& strs) {
vector> result;
std::sort(strs.begin(), strs.end());
map> codeToStrs;
for(int i = 0; i>::iterator it = codeToStrs.begin(); it!= codeToStrs.end(); it++){
result.push_back(it->second);
}
return result;
}
string getCode(string s){
std::sort(s.begin(), s.end());
return s;
}
};
原文作者:;如有侵权,请联系管理员删除。