mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-04 03:34:21 +00:00
772183705e
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
39 lines
1.4 KiB
Go
39 lines
1.4 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
|
|
}
|