Skip to content

Files

Latest commit

b5c378c · May 9, 2018

History

History
44 lines (31 loc) · 953 Bytes

049._group_anagrams_python.md

File metadata and controls

44 lines (31 loc) · 953 Bytes

###49. Group Anagrams python

题目: https://leetcode.com/problems/anagrams/

难度 : Medium

我又来使用我的取巧神奇python大法

思路是: 首先我们把这个单词都变成排序后的str,然后把它做key放入字典中,所以一样的就会放到同样的key下面,最后再把字典的值全部聚集起来。

class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        mapx = {}
        for str1 in strs:
           key = self.sortedWord(str1)
           if key in mapx:
               mapx[key].append(str1)
           else:
               mapx[key] = [str1]
        return list(mapx.values()) 
        
    def sortedWord(self,s):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        sList = sorted(list(s))
        str1 = ''.join(sList)
        return str1