算法训练营day23(补),回溯3

import (

"sort"

)

39. 组合总和

func combinationSum(candidates \[\]int, target int) \[\]\[\]int {

//存储全部集合

result := make(\[\]\[\]int, 0)

if len(candidates) == 0 {

return result

}

sort.Ints(candidates) //排序后面做剪枝

//存储单次集合

path := make(\[\]int, 0)

var backtrace func(candidates \[\]int, target int, startIndex int)

backtrace = func(candidates \[\]int, target int, startIndex int) {

if target == 0 {

temp := make(\[\]int, len(path))

copy(temp, path)

result = append(result, temp)

return

}

for i := startIndex; i < len(candidates); i++ {

if candidatesi > target { //剪枝

break

}

path = append(path, candidatesi)

backtrace(candidates, target-candidatesi, i)

//回溯处理

path = path:len(path)-1

}

}

backtrace(candidates, target, 0)

return result

}

40. 组合总和 II

func combinationSum2(candidates \[\]int, target int) \[\]\[\]int {

//存储全部集合

result := make(\[\]\[\]int, 0)

if len(candidates) == 0 {

return result

}

sort.Ints(candidates) //排序后面做剪枝

//记录数组每一个元素是否使用过

user := make(\[\]bool, len(candidates))

//存储单次集合

path := make(\[\]int, 0)

var backtrace func(candidates \[\]int, target int, startIndex int)

backtrace = func(candidates \[\]int, target int, startIndex int) {

if target == 0 {

temp := make(\[\]int, len(path))

copy(temp, path)

result = append(result, temp)

return

}

for i := startIndex; i < len(candidates); i++ {

if candidatesi > target { //剪枝

break

}

if i > 0 && candidatesi == candidatesi-1 && useri-1 == false { //过滤重复

continue

}

path = append(path, candidatesi)

useri = true

backtrace(candidates, target-candidatesi, i+1)

//回溯处理

path = path:len(path)-1

useri = false

}

}

backtrace(candidates, target, 0)

return result

}

//判断是否是回文

func isPalindrome(s string) bool {

for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {

if si != sj {

return false

}

}

return true

}

131. 分割回文串

func partition(s string) \[\]\[\]string {

//存储全部集合

result := make(\[\]\[\]string, 0)

if len(s) == 0 {

return result

}

//存储单次集合

path := make(\[\]string, 0)

var backtrace func(se string, startIndex int)

backtrace = func(se string, startIndex int) {

if startIndex == len(se) {

temp := make(\[\]string, len(path))

copy(temp, path)

result = append(result, temp)

return

}

for i := startIndex; i < len(se); i++ {

//对字符串进行切割

str := sestartIndex : i+1

if isPalindrome(str) {

path = append(path, str)

backtrace(se, i+1)

//回溯处理

path = path:len(path)-1

}

}

}

backtrace(s, 0)

return result

}

相关推荐
小小测试开发4 小时前
安装 Python 3.10+
开发语言·人工智能·python
KaMeidebaby5 小时前
卡梅德生物技术快报|PD1 单克隆抗体定制配套 N 糖全谱质控开发
前端·人工智能·算法·数据挖掘·数据分析
8Qi85 小时前
LeetCode 235. 二叉搜索树的最近公共祖先(LCA)
算法·leetcode·二叉树·递归·二叉搜索树·lca·迭代
AAA大运重卡何师傅(专跑国道)6 小时前
【无标题】
开发语言·c#
bIo7lyA8v6 小时前
算法稳定性分析中的随机扰动建模的技术8
算法
sugar__salt6 小时前
从栈队列数据结构到JS原型面向对象全解
前端·javascript·数据结构
XBodhi.6 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
科研online6 小时前
基于多源数据和XGBoost-SHAP分析中国大陆绿地碳汇空间变异影响因素的非线性相关性与尺度差异
算法·学习方法
Cthy_hy7 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.7 小时前
【01】Python 机器学习
开发语言·python