我想通过分隔符“/”扩展一串切片。例如,扩展以下切片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
}
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报
0/150
提交
取消