题目 签到题 c++ class Solution {public: mapstring,int m; vectorvectorstring groupAnagrams(vectorstring strs) { vectorstring strs2 = strs; for(int i=0;istrs.size();i++) { sort(strs[i].begin(),strs[i].end()); } vectorvectorstring res; in
题目
签到题
c++
class Solution { public: map<string,int> m; vector<vector<string>> groupAnagrams(vector<string>& strs) { vector<string> strs2 = strs; for(int i=0;i<strs.size();i++) { sort(strs[i].begin(),strs[i].end()); } vector<vector<string>> res; int pos=0; vector<string> ans; for(int i=0;i<strs.size();i++) { if(m[strs[i]]==0) { m[strs[i]]=++pos; ans.clear(); ans.push_back(strs2[i]); res.push_back(ans); } else { res[m[strs[i]]-1].push_back(strs2[i]); } } return res; } };