leetcode49 Group Anagrams
Posted yawenw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode49 Group Anagrams相关的知识,希望对你有一定的参考价值。
1 """ 2 Given an array of strings, group anagrams together. 3 Example: 4 Input: ["eat", "tea", "tan", "ate", "nat", "bat"], 5 Output: 6 [ 7 ["ate","eat","tea"], 8 ["nat","tan"], 9 ["bat"] 10 ] 11 """ 12 class Solution: 13 def groupAnagrams(self, strs): 14 d = {} 15 for s in strs: 16 key = tuple(sorted(s))#!!! 17 d[key] = d.get(key, []) + [s] #!!! 18 #dict.get(key, default=None) 19 #key -- 字典中要查找的键。 20 #default -- 如果指定键的值不存在时,返回该默认值 21 return d.values()
以上是关于leetcode49 Group Anagrams的主要内容,如果未能解决你的问题,请参考以下文章
一天一道LeetCode#49. Group Anagrams