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())
相关推荐
Nil2082 分钟前
leetcode 189轮转数组
数据结构·算法·leetcode
吃着火锅x唱着歌37 分钟前
LeetCode 3885.设计事件管理器
算法·leetcode·职场和发展
土司大王1 小时前
LeetCode hto100——字母异位词分组
java·算法·leetcode
土司大王2 小时前
LeetCode hot100——两数之和
数据结构·算法·leetcode
Navigator_Z3 小时前
LeetCode //C - 1200. Minimum Absolute Difference
c语言·算法·leetcode
wabs6663 小时前
关于字符串【力扣344.反转字符串的思考】
数据结构·算法·leetcode
Nil2084 小时前
leetcode 73矩阵置0
算法·leetcode·矩阵
旖旎夜光6 小时前
LeetCode 1658:将 x 减到 0 的最小操作数(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
ZC跨境爬虫7 小时前
LeetCode 88. 合并两个有序数组(双指针详解 + Java Python 实现)
java·python·算法·leetcode
Nil2087 小时前
leetcode 238除了自身以外数组的乘积
数据结构·算法·leetcode