Go實現笛卡爾積

var countIndex = len(sep) - 1app

var counter = make([]int, countIndex+1)

var combinations [][]string

var firstSize = len(sep[0]) - 1
for firstSize > counter[0] {

	combination := make([]string, countIndex+1)
	for i := countIndex; i >= 0; i-- {
		combination = append(combination, sep[i][counter[i]])
	}
	combinations = append(combinations, combination)

	counter[countIndex]++
	for i := countIndex; i >= 0; i-- {
		if counter[i] > len(sep[i])-1 {
			counter[i] = 0
			if i > 0 {
				counter[i-1]++
			}
		}
	}
}
相關文章
相關標籤/搜索