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

通过分隔符扩展一串切片

通过分隔符扩展一串切片

Go
皈依舞 2023-05-15 15:00:20
我想通过分隔符“/”扩展一串切片。例如,扩展以下切片s := []string{"5/3","9","5/4/1","6"}应该产生单独的切片:["5","9","5","6"] ["5","9","4","6"] ["5","9","1","6"]["3","9","5","6"] ["3","9","4","6"] ["3","9","1","6"]我几乎被困在这里var c [][]string{}s := []string{"5/3","9","5/4/1","6"}for _, v := range s {    combos := strings.Split(v, "/")    for _, combo := range combos {    }       }
查看完整描述

1 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

除了运行时间,您可以通过递归来实现。


func Perm(digits [][]string) (perm [][]string) {


    if len(digits) == 0 || len(digits) == 1 {

        return digits

    }


    nextDigits := Perm(digits[1:])

    for _, digit := range digits[0] {

        for _, next := range nextDigits {

            cat := append([]string{digit}, next...)

            perm = append(perm, cat)

        }

    }


    return perm

}

操场


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

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信