perf: 优化 slice.Combinations 效率
This commit is contained in:
parent
2482d2e7f0
commit
03028b1a41
|
@ -1,7 +1,6 @@
|
|||
package slice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
)
|
||||
|
@ -221,27 +220,13 @@ func GetIndexAny[V any](slice []V, values V) int {
|
|||
|
||||
// Combinations 获取给定数组的所有组合,包括重复元素的组合
|
||||
func Combinations[T any](a []T) [][]T {
|
||||
n := len(a)
|
||||
|
||||
// 去除重复元素,保留唯一元素
|
||||
uniqueSet := make(map[string]bool)
|
||||
uniqueSlice := make([]T, 0, n)
|
||||
for _, val := range a {
|
||||
ptr := fmt.Sprintf("%p", val)
|
||||
if !uniqueSet[ptr] {
|
||||
uniqueSet[ptr] = true
|
||||
uniqueSlice = append(uniqueSlice, val)
|
||||
}
|
||||
}
|
||||
|
||||
n = len(uniqueSlice) // 去重后的数组长度
|
||||
totalCombinations := 1 << n // 2的n次方
|
||||
var result [][]T
|
||||
for i := 0; i < totalCombinations; i++ {
|
||||
n := len(a)
|
||||
for i := 0; i < (1 << n); i++ {
|
||||
var currentCombination []T
|
||||
for j := 0; j < n; j++ {
|
||||
if (i & (1 << j)) != 0 {
|
||||
currentCombination = append(currentCombination, uniqueSlice[j])
|
||||
currentCombination = append(currentCombination, a[j])
|
||||
}
|
||||
}
|
||||
result = append(result, currentCombination)
|
||||
|
|
Loading…
Reference in New Issue