为了账号安全,请及时绑定邮箱和手机立即绑定

Golang 打印数组数组的所有排列

Golang 打印数组数组的所有排列

Go
ibeautiful 2023-02-06 19:40:33
寻找一种方法来打印数组数组的所有组合。数据类型看起来像这样:// print all combos var data [][]string例子输入:[[Lorem, Itself], [Alpha, Beta, Theta]]预期输出:Alpha Alpha、Alpha Beta、Alpha Theta、非常 Alpha、非常 Beta、非常 Theta数组可以是任意长度。完成此任务的最佳方法是什么?
查看完整描述

1 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

我会迭代切片索引的向量。


单索引迭代器:


// Iterator of a slice index. `len` equals to the length of the slice

type IdxIter struct {

    idx uint

    len uint

}


// Returns true is the iteration is over.

func (i IdxIter) Done() bool {

    return i.idx >= i.len

}


// Builds the next iteration value. If called for the last index,

// the next value's `Done` returns `true`.

func (i *IdxIter) Next() {

    i.idx++

}


// Resets the iterator

func (i *IdxIter) Reset() {

    i.idx = 0

}


// The index value

func (i IdxIter) Idx() uint {

    return i.idx

}

索引向量的迭代器:


// Index iterator for a slice of slices

type IdxVectorIter []IdxIter


// Returns true is the iteration is over.

func (ii IdxVectorIter) Done() bool {

    last := len(ii) - 1

    return ii[last].Done()

}


// Builds the next iteration value. If called for the last index vector,

// the next value's `Done` returns `true`.

func (ii IdxVectorIter) Next() {

    if len(ii) == 0 {

        return

    }

    last := len(ii) - 1

    for pos := range ii[:last] {

        ii[pos].Next()

        if ii[pos].Done() {

            ii[pos].Reset()

        } else {

            return

        }

    }

    ii[last].Next()

}

这样,切片切片的迭代就很简单了:


func main() {

    words := [][]string{

        {"lorem", "ipsum"},

        {},

        {"Alpha", "Beta", "Gamma"},

        {"X", "Y", "Z"},

    }

    // Fixed buffer for the combinations of words

    dst := make([]string, len(words))

    // Iteration loop

    for ii := NewIdxVectorFromSlices(words); !ii.Done(); ii.Next() {

        GetTo(words, dst, ii)

        fmt.Printf("%v\n", dst)

    }

}

完整代码https://go.dev/play/p/ecjjcAEexZO


输出


[lorem  Alpha X]

[ipsum  Alpha X]

[lorem  Beta X]

[ipsum  Beta X]

[lorem  Gamma X]

[ipsum  Gamma X]

[lorem  Alpha Y]

[ipsum  Alpha Y]

[lorem  Beta Y]

[ipsum  Beta Y]

[lorem  Gamma Y]

[ipsum  Gamma Y]

[lorem  Alpha Z]

[ipsum  Alpha Z]

[lorem  Beta Z]

[ipsum  Beta Z]

[lorem  Gamma Z]

[ipsum  Gamma Z]


查看完整回答
反对 回复 2023-02-06
  • 1 回答
  • 0 关注
  • 201 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号