当前位置: 首页 > article >正文

【代码随想录】算法训练计划27

回溯

1、39. 组合总和

题目:
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]

思路:
  • 第二次写了,很简单就写出来了
  • 注意 2 可以使用多次。_backtrack(res, list,candidates, target,sum, i+1),不要i+1
func combinationSum(candidates []int, target int) [][]int {
    // 代码二刷,很经典,回溯
    list := make([]int, 0)
    res := make([][]int, 0)
    backtrack(&res, list,candidates,target,0,0)
    return res
}
func backtrack(res *[][]int, list,candidates []int, target, sum,index int) {
    if sum == target {
        ans := make([]int, len(list))
        copy(ans, list)
        *res = append(*res, ans)
        return
    }
    if sum > target {return}
    for i := index; i<len(candidates); i++ {
        sum += candidates[i]
        list = append(list, candidates[i])
        backtrack(res, list,candidates, target,sum, i)
        sum -= candidates[i]
        list = list[:len(list)-1]
    }
}

2、40. 组合总和 II

题目:
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

思路:
  • 怎么去重呢?,使用 history
  • else if sum > target 记得这个也要剪枝,避免无效运算导致的超时

在这里插入图片描述

func combinationSum2(candidates []int, target int) [][]int {
    // 代码二刷
    sort.Ints(candidates)
    history := make([]bool, len(candidates))
    list := make([]int, 0)
    res := make([][]int, 0)
    backtrack(&res, list,candidates,target,0,0,history)
    return res
}
func backtrack(res *[][]int, list, candidates []int, target, index, sum int,history []bool) {
    if sum == target {
        ans := make([]int, len(list))
        copy(ans, list)
        *res = append(*res, ans)
        return
    } else if sum > target {
        return
    }
    for i:=index; i<len(candidates); i++ {
        if i>=1 && candidates[i] == candidates[i-1] && history[i-1] == false {
            continue
        }
        sum += candidates[i]
        list = append(list, candidates[i])
        history[i] = true
        backtrack(res, list, candidates, target, i+1, sum,history)
        sum -= candidates[i]
        list = list[:len(list)-1]
        history[i] = false
    }
}

3、131. 分割回文串

题目:
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]

思路:
  • 细节,注意:i:=index
  • 注意其实回文很简单,带入例子就明白了
  • *res = append(*res, ans),注意是 ans,为什么呢?
func partition(s string) [][]string {
    // 代码二刷,条件就是判断是否为回文字符串
    list := make([]string, 0)
    res := make([][]string, 0)
    backtrack(&res, list, s, 0)
    return res
}
func backtrack(res *[][]string, list []string, s string, index int) {
    if index == len(s) {
        ans := make([]string, len(list))
        copy(ans, list)
        *res = append(*res, ans)
        return
    }
    for i:=index; i<len(s); i++ {
        if huiwen(s, index, i) {
            list = append(list, s[index:i+1])
            backtrack(res, list, s, i+1)
            list = list[:len(list)-1]
        }
    }
}
func huiwen(s string, left,right int) bool {
    for left<right {
        if s[left] != s[right] {
            return false
        }
        left++
        right--
    }
    return true
}

http://www.kler.cn/news/135822.html

相关文章:

  • springboot引入redisson分布式锁及原理
  • 深度学习之基于YoloV5-Pose的人体姿态检测可视化系统
  • 产品经理进阶:外包原因及类型(一)
  • Java获取Jar、War包路径,并生成可编辑修改的本地配置文件
  • 05-networkX-结构洞计算
  • Halcon Solution Guide I basics(2): Image Acquisition(图像加载)
  • 2024年csdn最新最全的Postman接口测试: postman定义公共函数
  • 锐捷OSPF认证
  • Postman启动问题:Could not open Postman
  • BLE协议栈入门学习
  • DSP2335的按键输入key工程笔记
  • element表格头部加入图标
  • Vue中实现div的任意移动
  • 前端为什么要工程化
  • 计算矩阵边缘元素之和
  • 七天.NET 8操作SQLite入门到实战 - SQLite 简介
  • STM32电源名词解析
  • 持续集成部署-k8s-配置与存储-配置管理:ConfigMap 的热更新
  • 使用1688开放平台API接口获取商品详情信息
  • Java值传递和引用传递
  • VAD监测(一)
  • 最全HTTP/HTTPS面试题整理(二)
  • 【开源】基于JAVA的快递管理系统
  • Uniapp连接iBeacon设备——实现无线定位与互动体验(实现篇)
  • 基础组件-流量回放(全链路流量回放预研)
  • 论文-分布式-拜占庭将军问题
  • 免疫微环境、免疫细胞浸润分析、免疫功能分析
  • 解决在pycharm中使用matplotlib画图问题
  • SpringBean的配置详解 --中
  • UnitTest框架