python leetcode 49. Group Anagrams

对每个字符串进行排序,那么排序后Anagrams的字符串是相同的

class Solution:
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        dict1={}
        for i in range(len(strs)):
            strsort = ''.join(sorted(strs[i]))
            if not strsort in dict1:
                dict1[strsort]=[i]
            else:
                dict1[strsort].append(i)
        res=[]
        for list1 in dict1.values():
            temp=[]
            for index in list1:
                temp.append(strs[index])
            res.append(temp)
        return res

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84891229
今日推荐