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

题目:

题解:

python 复制代码
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())
相关推荐
小徐学编程-zZ2 小时前
量产测试数据
python·压力测试·数据库架构
QQ8057806512 小时前
django基于机器学习的电商评论情感分析系统设计实现
python·机器学习·django
wx09092 小时前
stata实现机器学习的环境配置
python·机器学习·stata
洛水水3 小时前
【力扣100题】30.二叉树的直径
算法·leetcode·职场和发展
nuowenyadelunwen4 小时前
CS 61A Lab 2 笔记:短路求值、高阶函数与 Lambda 表达式
python·函数式编程·cs61a·berkeley
qq_422828625 小时前
android图形学之SurfaceControl和Surface的关系 五
android·开发语言·python
weixin_444012935 小时前
c++如何将std--vector直接DUMP到二进制文件_指针地址直写【附代码】
jvm·数据库·python
洛水水5 小时前
【力扣100题】32.将有序数组转换为二叉搜索树
数据结构·算法·leetcode
woxihuan1234566 小时前
Go语言中--=运算符详解:位右移赋值操作的原理与应用
jvm·数据库·python
石山代码6 小时前
Python 数据分析三大库:NumPy + Pandas + Matplotlib
python·数据分析·numpy