Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] Note:
All inputs will be in lowercase. The order of your output does not matter.
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/group-anagrams 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
因为map会自动排序,所以一切都自动调整好了
把字符串sort后放入对应map,自动就排好了、,最后pushback。。
代码语言:javascript复制class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
map<string,vector<string> > ma;
vector<vector<string>> res;
for(auto str:strs){
string tmp = str;
sort(tmp.begin(),tmp.end());
ma[tmp].push_back(str);
}
for(const auto& m:ma)
res.push_back(m.second);
return res;
}
};