Go实现笛卡尔积

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

var countIndex = len(sep) - 1

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]++
			}
		}
	}
}

转载于:https://my.oschina.net/u/1023800/blog/736671

你可能感兴趣的:(Go实现笛卡尔积)