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())
相关推荐
chushiyunen10 分钟前
python中的魔术方法(双下划线)
前端·javascript·python
深蓝轨迹19 分钟前
@Autowired与@Resource:Spring依赖注入注解核心差异剖析
java·python·spring·注解
人工智能AI技术23 分钟前
Python 3.14.3更新!内存优化与安全补丁实战应用
python
2401_8916558126 分钟前
此电脑网络位置异常的AD域排错指南的技术文章大纲
开发语言·python·算法
不要秃头的小孩38 分钟前
50. 随机数排序
数据结构·python·算法
qq_417695051 小时前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python
1941s1 小时前
Google Agent Development Kit (ADK) 指南 第五章:工具集成与自定义
人工智能·python·langchain·agent·adk
故事和你911 小时前
sdut-python-实验四-python序列结构(21-27)
大数据·开发语言·数据结构·python·算法
memcpy01 小时前
LeetCode 1456. 定长子串中元音的最大数目【定长滑窗模板题】中等
算法·leetcode·职场和发展
chushiyunen1 小时前
pycharm注意力残差示例
ide·python·pycharm