49. 字母异位词分组

49. 字母异位词分组,第1张

49. 字母异位词分组

排列复杂度太高

class Solution {
public:
    vector> groupAnagrams(vector& strs) {
	unordered_map>a;
	for(auto str : strs)
	{
		auto str1=str;
		sort(str1.begin(), str1.end());
		auto it = a.find(str1);
		if(it==a.end())
			a.insert(pair>(str1, vector{str}));
		else
			it->second.push_back(str);
	}
	vector>ret;
	for(auto x:a)
	{
		ret.push_back(x.second);
	}
	return ret;

    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5670493.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存