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())
相关推荐
min(a,b)14 小时前
学习第 6 天:类型注解、装饰器与高级特性
python·学习
码云骑士14 小时前
80-指令微调Instruction-Tuning-指令数据构造-多任务训练-过拟合检测
python
白白白小纯14 小时前
算法篇—返回倒数第k个节点
c语言·数据结构·算法·leetcode
爱昏羔15 小时前
上篇:从PDF到向量库 — 物流行业RAG系统的知识库构建全解析
python·langchain·pdf·agent·rag
麻雀飞吧15 小时前
最新量化实现难点,规则和流程要先有形状
人工智能·python
用户03321266636715 小时前
使用 Python 在 Word 文档中添加或删除文本框
python
LadenKiller16 小时前
近期AI量化辅助,工具重点要跟学习阶段变化
人工智能·python
白白白小纯16 小时前
算法篇—链表的中间节点
c语言·数据结构·算法·leetcode
phltxy16 小时前
LangChain_Agent中间件实战
人工智能·python·深度学习·语言模型·中间件·langchain
Frostnova丶16 小时前
(19)LeetCode 54. 螺旋矩阵
算法·leetcode·矩阵