2026.01.22 组合 &

https://leetcode.cn/problems/combinations/description/

python 复制代码
class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        ans = []
        path = []

        def dfs(n: int) -> None:
            d = k - len(path)
            # d表示还需要的长度
            if d == 0:
                ans.append(path.copy())
                return
            for i in range(n, d - 1, -1):
                # 左开右闭,当剩余的数字不足够d(所需要的长度)时结束循环
                path.append(i)
                dfs(i - 1)
                path.pop()
        dfs(n)
        return ans

https://leetcode.cn/problems/combination-sum-iii/description/

python 复制代码
class Solution:
    def combinationSum3(self, k: int, n: int) -> List[List[int]]:
        ans = []
        path = []
        curSum = 0
        def dfs(i:int) -> None:
            nonlocal curSum
            d = n - curSum
            if k - len(path) > i or d < 0:
                # 剪枝
                return
            if d == 0 and len(path) == k:
                ans.append(path[:])
                return
            
            for j in range(i, 0, -1):
                curSum += j
                path.append(j)
                dfs(j - 1)
                curSum -= j
                path.pop()
        dfs(9)
        return ans

https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/

python 复制代码
class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        if not digits: return []
        res = []
        phone = {'2':['a','b','c'],
                 '3':['d','e','f'],
                 '4':['g','h','i'],
                 '5':['j','k','l'],
                 '6':['m','n','o'],
                 '7':['p','q','r','s'],
                 '8':['t','u','v'],
                 '9':['w','x','y','z']}

        def backtrack(conbination, nextdigit):
            if len(nextdigit) == 0:
                res.append(conbination)
                return
            else:
                for letter in phone[nextdigit[0]]:
                    backtrack(conbination + letter, nextdigit[1:])
        backtrack('', digits)
        return res
相关推荐
普贤莲花3 分钟前
【2026年第18周---写于20260501】---舍得
程序人生·算法·leetcode
2zcode3 分钟前
基于深度学习的口腔疾病图像识别系统(UI界面+改进算法+数据集+训练代码)
人工智能·深度学习·算法
Sarvartha12 分钟前
N 个字符串最长公共子序列(LCS)求解问题
数据结构·算法
一切皆是因缘际会12 分钟前
下一代 AI 架构:基于记忆演化与单向投影的安全智能系统
大数据·人工智能·深度学习·算法·安全·架构
falldeep19 分钟前
五分钟了解OpenClaw底层架构
人工智能·算法·机器学习·架构
m0_6294947319 分钟前
LeetCode 热题 100-----16.除了自身以外数组的乘积
数据结构·算法·leetcode
weixin_4462608525 分钟前
模型能力深度对决:GPT-4o、Claude 3.5和DeepSeek V系列模型的横向评测与未来趋势洞察
人工智能·算法·机器学习
想唱rap1 小时前
应用层协议与序列化
linux·运维·服务器·网络·数据结构·c++·算法
重生之我是Java开发战士1 小时前
【笔试强训】Week3:重排字符串,分组,DNA序列
算法
We་ct1 小时前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·leetcode·typescript·动态规划