LeetCode热题100- 字母异位词分组

给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

看到题目想到对字符串排序不难,但是需要对结果进行去重,去重方法其实没想到,需要使用字典去重,然后将字段的values获得最终结果。

python 复制代码
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        if not strs:
            return [[]]
        
        d = {}
        for s in strs:
            char = ''.join(sorted(s))
            if char not in d:
                d[char] = []
            d[char].append(s)
        
        return list(d.values())
相关推荐
晒太羊的猫9 分钟前
hot100子串
java·算法·leetcode
海清河晏1111 小时前
加油站问题
数据结构·算法·leetcode
Nuanyt2 小时前
LeetCode Hot 100 · 双指针 3 题 Java
java·笔记·算法·leetcode·双指针
文丑颜不良啊4 小时前
LeetCode851~875题目解析
java·leetcode
Navigator_Z4 小时前
LeetCode //C - 1304. Find N Unique Integers Sum up to Zero
c语言·算法·leetcode
?abc!16 小时前
(数组)581. 最短无序连续子数组
数据结构·算法·leetcode
hold?fish:palm19 小时前
40 二叉树的直径
c++·leetcode
guo_wen_qiang19 小时前
二叉树的层序遍历-使用双端队列Deque
算法·leetcode·1024程序员节
爱读书的小胖20 小时前
LeetCode 55 跳跃游戏 C 语言解题分析
c语言·数据结构·算法·leetcode·1024程序员节
Navigator_Z21 小时前
LeetCode //C - 1300. Sum of Mutated Array Closest to Target
c语言·算法·leetcode