算法训练营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

}

相关推荐
会周易的程序员9 分钟前
js-shm: 高性能 Node.js 共享内存模块
开发语言·javascript·c++·node.js·共享内存·shm
江湖十年19 分钟前
Go 语言中 YAML to JSON 踩坑笔记
后端·go·json
笨蛋不要掉眼泪22 分钟前
Java虚拟机:常用参数
java·开发语言·python
happy_0x3f35 分钟前
前端应用的离线暂停更新策略
开发语言·前端·php
shylyly_1 小时前
C++中的类型转换
开发语言·c++·匿名对象·隐式类型转换·拷贝优化
北冥you鱼2 小时前
Go 语言新手扫盲:指针 * 和 & 使用场景详解
开发语言·后端·golang
cui_ruicheng2 小时前
Python数据分析(一):数据分析概述与环境搭建
开发语言·python·数据分析
青山木2 小时前
Hot 100 ---腐烂的橘子
java·数据结构·后端·算法·leetcode·广度优先
未来之窗软件服务2 小时前
计算机考试-快速排序—东方仙盟
数据结构·算法·排序算法·仙盟创梦ide·东方仙盟
心平气和量大福大2 小时前
C#-WPF-UserControl-生命周期(加载 退出)
开发语言·c#·wpf