猿问

通过分隔符扩展一串切片

我想通过分隔符“/”扩展一串切片。


例如,扩展以下切片


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 {


    }       

}


皈依舞
浏览 97回答 1
1回答

四季花海

除了运行时间,您可以通过递归来实现。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}操场
随时随地看视频慕课网APP

相关分类

Go
我要回答