小弟最近刚刚开始学GO语言,正在Leetcoding上刷题,今天刷一道Combinations题时遇到了困难。
![微信图片_20171102213523.png](https://static.studygolang.com/171102/86f1faf873ca94578d7bed63ed5605ef.png)
我的输出定义是二维slice`var result [][]int`
另外定义了stack来寻找符合条件的单维数组`stack := make([]int, 0, k)`
如果stack符合条件就附加入result中,`result = append(result, stack)`
每次执行append都做一次输出,但是输出的结果是
![微信图片_20171102213339.png](https://static.studygolang.com/171102/87dc365ee8ef3d6ee493b200f5fc63df.png)
请问我该怎么修改?
```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
更多评论
报错了额。。。cannot use stack (type []int) as type int in array or slice literal
#2