新手求教一个关于slice的问题

wumier · · 1668 次点击
```go func main() { n, k := 4, 2 stack := make([]int, 0, k) //var result [][]int var result = make([][]int, 0, 100) push := func(x int) { n := len(stack) stack = stack[:n+1] stack[n] = x return } pop := func() int { n := len(stack) x := stack[n-1] stack = stack[:n-1] return x } temp := 1 for { if len(stack) == k { result = append(result, []int{stack}) fmt.Println(result) if stack[0] == n-k+1 { break } pop() } else { if temp <= n { push(temp) temp++ } else { temp = pop() + 1 } } } fmt.Println(result) } ```
#6
更多评论
试试 result = append(result, []int{stack})
#1
报错了额。。。cannot use stack (type []int) as type int in array or slice literal
#2