算法训练营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 candidates[i] > target { //剪枝

break

}

path = append(path, candidates[i])

backtrace(candidates, target-candidates[i], 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 candidates[i] > target { //剪枝

break

}

if i > 0 && candidates[i] == candidates[i-1] && user[i-1] == false { //过滤重复

continue

}

path = append(path, candidates[i])

user[i] = true

backtrace(candidates, target-candidates[i], i+1)

//回溯处理

path = path[:len(path)-1]

user[i] = 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 s[i] != s[j] {

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 := se[startIndex : i+1]

if isPalindrome(str) {

path = append(path, str)

backtrace(se, i+1)

//回溯处理

path = path[:len(path)-1]

}

}

}

backtrace(s, 0)

return result

}

相关推荐
potato_may10 分钟前
CC++ 内存管理 —— 程序的“五脏六腑”在哪里?
c语言·开发语言·数据结构·c++·内存·内存管理
饕餮怪程序猿17 分钟前
A*算法(C++实现)
开发语言·c++·算法
电饭叔21 分钟前
不含Luhn算法《python语言程序设计》2018版--第8章14题利用字符串输入作为一个信用卡号之二(识别卡号有效)
java·python·算法
观音山保我别报错38 分钟前
列表,元组,字典
开发语言·python
**蓝桉**1 小时前
数组的执行原理,java程序的执行原理
java·开发语言
2301_800256111 小时前
8.2 空间查询基本组件 核心知识点总结
数据库·人工智能·算法
不穿格子的程序员1 小时前
从零开始写算法——矩阵类题:矩阵置零 + 螺旋矩阵
线性代数·算法·矩阵
waeng_luo1 小时前
[鸿蒙2025领航者闯关] 表单验证与用户输入处理最佳实践
开发语言·前端·鸿蒙·鸿蒙2025领航者闯关·鸿蒙6实战·开发者年度总结
高频交易dragon1 小时前
5分钟和30分钟联立进行缠论信号分析
开发语言·python
ULTRA??1 小时前
C/C++函数指针
c语言·开发语言·c++