字母易位词
1、预备知识
A STL
https://blog.csdn.net/u010183728/article/details/81913729
!1 STL的一个重要特点是数据结构和算法的分离
!2 STL另一个重要特性是它不是面向对象的
!3 从逻辑层次来看,在STL中体现了泛型化程序设计的思想
B map /multimap
Map/Multimap:
Map的元素是成对的键值/实值,内部的元素依据其值自动排序,Map内的相同数值的元素只能出现一次,Multimaps内可包含多个数值相同的元素,内部由二叉树实现,便于查找;
C unodered_map/unodered_mutimap
https://www.cnblogs.com/tp-16b/p/9156810.html
2、实现代码
网友版
class Solution {
public:
vector<vector> groupAnagrams(vector& strs) {
vector<vector> res;
//定义关联类unordered_map容器对象
unordered_map<string, vector> m;
//遍历strs中的所有字符串
for (string str : strs) {
string t = str;
sort(t.begin(), t.end());
//在m尾部插入一个元素
m[t].push_back(str);
}
//用a遍历m中的所有对象
for (auto a : m) {
//The elements of a map<string,int> are of type pair<string,int>;
//The first member of a pair is called first and the second member second.
res.push_back(a.second);
}
return res;
}
};
正式版
思想:两个字符串若有相同的字母 那么就会将相同字母位置的值给去掉
、