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())
相关推荐
Artech4 分钟前
我所理解的Python元模型
python·meta class·meta model
寒山-居士5 分钟前
量化客户端核心业务解析
python·金融
ths5127 分钟前
测试开发python中正则表达式使用总结(二)
开发语言·python·算法
heimeiyingwang8 分钟前
【架构实战】API接口防刷与限流策略
开发语言·python·架构
小白学大数据21 分钟前
告别复杂 XPath:DeepSeek+Python 爬虫快速实践
开发语言·爬虫·python·selenium
AI_Claude_code29 分钟前
ZLibrary访问困境方案六:自建RSS/Calibre内容同步服务器的完整指南
运维·服务器·网络·爬虫·python·tcp/ip·http
weixin_4620223532 分钟前
Dancing under the stars: video denoising in starlight
python·计算机视觉
人道领域32 分钟前
【LeetCode刷题日记】:从 LeetCode 经典题看哈希表的场景化应用---数组、HashSet、HashMap 选型与算法实战
算法·leetcode·面试
承渊政道34 分钟前
【优选算法】(实战攻坚BFS之FloodFill、最短路径问题、多源BFS以及解决拓扑排序)
数据结构·c++·笔记·学习·算法·leetcode·宽度优先
kishu_iOS&AI35 分钟前
机器学习 —— 线性回归(2)
人工智能·python·算法·机器学习·线性回归