题目

想法
使用hashmap:key存储的是 tuple(sorted(w)),value存储的是具有所有相同字母的异位词
代码
def groupAnagrams(self, strs):
d = {}
for w in sorted(strs):
key = tuple(sorted(w))
d[key] = d.get(key, []) + [w]
return d.values()
使用hashmap:key存储的是 tuple(sorted(w)),value存储的是具有所有相同字母的异位词
def groupAnagrams(self, strs):
d = {}
for w in sorted(strs):
key = tuple(sorted(w))
d[key] = d.get(key, []) + [w]
return d.values()
本文标题:leetcode49. 字母异位词分组
本文链接:https://www.haomeiwen.com/subject/anbotqtx.html
网友评论