题意很简单,,,就是把字母种类数量相同的字符串归并到一组中。
这里用map去区分是否为一组即可,并为每一组设置一个index。
注意:
如果出现reference binding to null pointer of type ‘value_type’” 这个报错
一般是数组下标的问题。
这题里我忘记给vector<vector<string>> ans 赋空间。
ans.push(NullAns) 这个忘了加了。
bool cmp(vector<string> a,vector<string> b){ return a.size() < b.size(); } class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { string sorts; vector<vector<string>> ans; int i; map<string, int> GetIndex; int Mindex = 0; int index; vector<string> nullans; for(i = 0;i < strs.size(); i++){ sorts = strs[i]; sort(sorts.begin(), sorts.end()); if(GetIndex.find(sorts) == GetIndex.end()){ GetIndex[sorts] = Mindex++; ans.push_back(nullans); } index = GetIndex[sorts]; ans[index].push_back(strs[i]); } for(i = 0;i < ans.size(); i++){ sort(ans[i].begin(),ans[i].end()); } sort(ans.begin(), ans.end(), cmp); return ans; } };