Group Anagrams

来源:互联网 发布:steam好玩的mac游戏 编辑:程序博客网 时间:2024/05/01 20:03

49. Group Anagrams

iven an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
Return:

[  ["ate", "eat","tea"],  ["nat","tan"],  ["bat"]]
https://discuss.leetcode.com/topic/26074/a-clean-c-solution-with-unordered_map

这里只要为每个字符串找到键值,然后分类就好。hash时,可以将排好序的字符串作为分类的依据,亦可以建一个vector<int>表,

统计字符串各个字母出现的次数。

这是将sort后的字符串作为key.

这是将统计好的vector<int>作为key.
一般排序的用时会更长些,统计是一次遍历

0 0
原创粉丝点击