回溯(子集型):分割回文串

一、多维递归 -> 回溯

1.1:17. 电话号码的字母组合(力扣hot100)

  • 代码:
python 复制代码
mapping = ["","", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        n = len(digits)
        if n == 0:
            return []
        res = []
        path = [""] * n
        def dfs(i):                              # 一个子问题的具体细节
            if i==n:
                res.append("".join(path))
                return
            for c in mapping[int(digits[i])]:
                path[i] = c
                dfs(i+1)                       # 下一个子问题
        dfs(0)
        return res

二、子集型回溯

2.1:78. 子集

  • 代码:
python 复制代码
class Solution:
	'''
	站在输入的角度,每个数都可选或不选
	最终每个叶子就是答案
	'''
    def subsets(self, nums: List[int]) -> List[List[int]]:
        if len(nums) == 0:
            return []
        res = []
        path = []
        n = len(nums)
        def dfs(i):
            if i==n:
                res.append(path.copy())
                return
            
            dfs(i+1)                # 不选

            path.append(nums[i])    # 选
            dfs(i+1)
            path.pop()   # 记得恢复现场
            
        dfs(0)
        return res

class Solution:
    """
    站在答案的角度,第一个数选谁,第二个数选谁,...
    每个节点都是答案子集
    """
    def subsets(self, nums: List[int]) -> List[List[int]]:
        if len(nums) == 0:
            return []
        res = []
        path = []
        n = len(nums)
        def dfs(i):
            res.append(path.copy())  # 每个节点都直接添加
            if i==n:
                return
            for j in range(i, n):
                path.append(nums[j])    # 下一个数选谁
                dfs(j+1)
                path.pop()   # 记得恢复现场
            
        dfs(0)
        return res

2.2:131. 分割回文串

  • 代码:
python 复制代码
class Solution:
    """
    站在答案的角度,第一个数选谁,第二个数选谁,...
    每个节点都是答案
    """
    def partition(self, s: str) -> List[List[str]]:
        res = []
        path = []
        n = len(s)
        def dfs(i):
            if i==n:
                res.append(path.copy())  # 这里是答案包含全部字符,所以只有最后一把才记录结果
                return 
            for j in range(i, n):
                t = s[i:j+1]
                if t==t[::-1]:
                    path.append(t)
                    dfs(j+1)
                    path.pop()
        dfs(0)
        return res
相关推荐
deep_drink2 分钟前
【经典论文精读(一)】Isomap:非线性降维的全局几何框架(Science 2000)
人工智能·算法·机器学习
不吃鱼的小时喵12 分钟前
【Python】关于python多进程
python
mjhcsp20 分钟前
莫比乌斯反演总结
c++·算法
喵手21 分钟前
Python爬虫零基础入门【第六章:增量、去重、断点续爬·第1节】增量采集:只抓新增/更新(新手也能做)!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·增量、去重·增量采集
万粉变现经纪人40 分钟前
如何解决 pip install pyodbc 报错 缺少 ‘cl.exe’ 或 ‘sql.h’(ODBC 头文件)问题
数据库·python·sql·网络协议·bug·ssl·pip
dazzle1 小时前
Python数据结构(五):队列详解
数据结构·python
翱翔的苍鹰1 小时前
完整的“RNN + jieba 中文情感分析”项目之一:需要添加添加 JWT 认证
人工智能·python·rnn
0思必得01 小时前
[Web自动化] 爬虫URL去重
运维·爬虫·python·selenium·自动化
爱编码的傅同学1 小时前
【今日算法】LeetCode 25.k个一组翻转链表 和 43.字符串相乘
算法·leetcode·链表
stolentime1 小时前
P14978 [USACO26JAN1] Mooclear Reactor S题解
数据结构·c++·算法·扫描线·usaco