标签:for inpu auto lower example div cond order back
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
基本的遍历练手题.
最开始试图用异或来做, 因为只要字母一样,不管顺序是什么 异或值是一样的..结果忘记了一个事情即使 异或值一样不能代表这俩string是一样的字母组成.
class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<string,vector<string>> res; for(int i=0;i<strs.size();++i) { string s= strs[i]; sort(s.begin(),s.end()); res[s].push_back(strs[i]); } vector<vector<string>> r; for(auto i=res.begin();i!=res.end();++i) { r.push_back(i->second); } return r; } };
标签:for inpu auto lower example div cond order back
原文地址:https://www.cnblogs.com/lychnis/p/11749213.html