LeetCode49.字母异位词分组(中等,分组)
作者:互联网
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
//总结:分组做法:建立这样的映射 map<string, vector<string>> mp!!!
map<string, vector<string>> mp; //映射:键为排序后的字符串,值为vector
vector<vector<string>> result;
string temp;
for (int i = 0; i < strs.size(); i++){
temp = strs[i];
sort(strs[i].begin(), strs[i].end()); //先排序
mp[strs[i]].push_back(temp);
}
for (map<string, vector<string>>::iterator it = mp.begin(); it != mp.end(); it++){
result.push_back(it -> second);
}
return result;
}
};
标签:map,temp,strs,异位,LeetCode49,vector,分组,mp,result 来源: https://blog.csdn.net/zhangjiaji111/article/details/111032941