思路: 字符串排序后作为key, 存到map中再转成vector返回, AC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { map<string, vector<string>> m; for(int i=0;i<strs.size();i++){ string s=strs[i]; sort(s.begin(),s.end()); m[s].push_back(strs[i]); } vector<vector<string>> res; for(auto &k:m){ res.push_back(k.second); } return res; } };
|