Python | Leetcode Python题解之第49题字母异位词分组

题目:

题解:

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        mp = collections.defaultdict(list)

        for st in strs:
            counts = [0] * 26
            for ch in st:
                counts[ord(ch) - ord("a")] += 1
            # 需要将 list 转换成 tuple 才能进行哈希
            mp[tuple(counts)].append(st)
        
        return list(mp.values())

相关推荐

  1. leetcode49字母分组

    2024-04-26 12:58:01       46 阅读
  2. 【算法49. 字母分组

    2024-04-26 12:58:01       58 阅读
  3. [ LeetCode ] 刷刷(Python)-49字母分组

    2024-04-26 12:58:01       35 阅读
  4. LeetCode 49 字母分组

    2024-04-26 12:58:01       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 12:58:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 12:58:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 12:58:01       82 阅读
  4. Python语言-面向对象

    2024-04-26 12:58:01       91 阅读

热门阅读

  1. css 将div固定在页面顶部不随页面滑动

    2024-04-26 12:58:01       34 阅读
  2. JVM 面试题

    2024-04-26 12:58:01       41 阅读
  3. 【对象存储】SpringBoot集成华为云OBS对象存储

    2024-04-26 12:58:01       31 阅读
  4. Vue3全局Api应用实例

    2024-04-26 12:58:01       30 阅读