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())
相关推荐
带多刺的玫瑰6 分钟前
Leecode#29刷题之两数相除
java·python·算法
smj2302_796826528 分钟前
解决leetcode第4017题数组中的峰值II
python·算法·leetcode
午彦琳12 分钟前
2026.9.9
数据结构·算法·leetcode
卷无止境13 分钟前
AI Agent编程中,重构节奏与安全检查的门道
后端·python
慧都小妮子18 分钟前
用 Python 批量把 PDF 参考文献排成 MLA 格式:Spire.Doc for Python 实战
python·pdf·c#·spire.doc·mla格式·参考文献排版·pdf批量处理
卷无止境27 分钟前
AI编程时代,代码复杂性正在悄悄失控
后端·python
开源量化GO34 分钟前
看到“2026年 Python 与 API”时,用示例和拆解看清关系
人工智能·python
高洁0138 分钟前
大模型是怎么“学会“的:预训练、微调、对齐三段论
python·深度学习·transformer·知识图谱·tornado
529宝宝起名网42 分钟前
用 Python 开发名字五行八字匹配工具:从八字排盘到喜用神起名的全流程实现
python
砚底藏山河1 小时前
并发与限频工程:把20只的2秒压到0.5秒不封号(魔码量化实战 #03)
java·开发语言·数据库·python·金融