查找表_leetcode49

class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
ans, res, ret = [], {}, []
for s in strs:
temp = list(s)
temp.sort()
ans.append("".join(temp))

for i in range(len(ans)):
if ans[i] not in res:
res[ans[i]] = []

res[ans[i]].append(strs[i])

for key in res:
ret.append(res[key])
return ret


strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
s = Solution()

print s.groupAnagrams(strs)

猜你喜欢

转载自www.cnblogs.com/lux-ace/p/10546906.html
今日推荐