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())
相关推荐
8Qi815 小时前
LeetCode热题100--45.跳跃游戏 II
java·算法·leetcode·贪心算法·编程
L-李俊漩16 小时前
荆华密算 面试题(大模型开发)
python
北顾笙98016 小时前
day12-数据结构力扣
数据结构·算法·leetcode
小陈工16 小时前
Python Web开发入门(十):数据库迁移与版本管理——让数据库变更可控可回滚
前端·数据库·人工智能·python·sql·云原生·架构
凌波粒16 小时前
LeetCode--454.四数相加 II(哈希表)
算法·leetcode·散列表
JoshRen16 小时前
Python中的简单爬虫
爬虫·python·信息可视化
永远的超音速16 小时前
PyCharm性能调优避坑录大纲
python·pycharm
AnchorYYC16 小时前
TEI Inference Toolkit - 工业级Embedding/NLI /Reranking服务调用最佳实践
人工智能·python·持续部署·#大模型应用
Ulyanov16 小时前
Pymunk 2D物理游戏开发教程系列 第二篇:约束与关节篇 -《摇摆特技车》
python·架构·系统仿真·雷达电子战·仿真引擎
Trouvaille ~16 小时前
零基础入门 LangChain 与 LangGraph(三):环境搭建、包安装与第一个 LangChain 程序
python·ai·chatgpt·langchain·大模型·openai·langgraph