mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 02:36:28 +08:00
a6b3f72826
* feat(go/backtracking): add go code * feat(backtracking): add n_queens in go * feat(backtracking): add /preorder_traversal_i_compact in go * feat(backtracking): add /preorder_traversal_ii_compact in go * feat(backtracking): add /preorder_traversal_ii_template in go * feat(backtracking): add preorder_traversal_iii_compact in go * feat(backtracking): add preorder_traversal_test in go * feat(backtracking): add permutations_i in go * feat(backtracking): add permutations_ii in go * feat(backtracking): add permutation_test in go * feat(backtracking): fix bug in go * Update permutations_i.go --------- Co-authored-by: Yudong Jin <krahets@163.com>
38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
// File: permutations_i.go
|
|
// Created Time: 2023-05-14
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_backtracking
|
|
|
|
/* 回溯算法:全排列 I */
|
|
func backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {
|
|
// 当状态长度等于元素数量时,记录解
|
|
if len(*state) == len(*choices) {
|
|
newState := append([]int{}, *state...)
|
|
*res = append(*res, newState)
|
|
}
|
|
// 遍历所有选择
|
|
for i := 0; i < len(*choices); i++ {
|
|
choice := (*choices)[i]
|
|
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
|
if !(*selected)[i] {
|
|
// 尝试:做出选择,更新状态
|
|
(*selected)[i] = true
|
|
*state = append(*state, choice)
|
|
// 进行下一轮选择
|
|
backtrackI(state, choices, selected, res)
|
|
// 回退:撤销选择,恢复到之前的状态
|
|
(*selected)[i] = false
|
|
*state = (*state)[:len(*state)-1]
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 全排列 I */
|
|
func permutationsI(nums []int) [][]int {
|
|
res := make([][]int, 0)
|
|
state := make([]int, 0)
|
|
selected := make([]bool, len(nums))
|
|
backtrackI(&state, &nums, &selected, &res)
|
|
return res
|
|
}
|