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())
相关推荐
钱栈up6 分钟前
奥迪上汽拍板「三年三款」,合资阵营的换挡节奏终于跟上了
爬虫·python·数据分析
东莞市云毅网络有限公司14 分钟前
SQLite FTS5 做中文问答检索:分词、排序与召回测试
python·sqlite·自动化运维·geo·数据监测
timeDifference₇10 小时前
Python Vibe Coding实操实验报告
开发语言·jvm·python
geovindu11 小时前
python:DecisionTree
开发语言·后端·python·算法
怒放的生命199112 小时前
代码知识库 RAG 问答系统 —— 从 0 到 1 实现教程
python·状态模式·fastapi·全栈开发·ai agent应用开发
geovindu13 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
fengkai454514 小时前
Python运维
运维·python·监控·psutil·ipy
Fancy.76114 小时前
:Python条件与循环语句学习总结
python
Wellijohn16 小时前
Python 容器选型实测:list.insert(0) 慢 925 倍,in 慢 6000 倍
python
新*16 小时前
标题全变成一级?靠编号规律找回章节边界
python·正则表达式·pdf解析·文本处理