dreamMyDream2014 2017-12-06 01:44
浏览 33
已采纳

切片附加更改切片中的其他元素

I'm solving Leetcode question at https://leetcode.com/problems/subsets with Golang and iterative solution. The algorithm is basically trying to add the new element to the existing array. After all, it will generate all of the subsets.

But when some elements added to the existing array solution, it change the other element also. Here's the code I provided. I put some debugging Println also.

package main

import (
    "fmt"
)

func main() {
    fmt.Println(subsets([]int{0, 1, 2, 3, 4}))
}

func subsets(nums []int) [][]int {
    res := [][]int{}

    res = append(res, []int{})
    for i := 0; i < len(nums); i++ {
        for j := range res {
            fmt.Println(i)
            temp := append(res[j], nums[i])
            fmt.Println(temp)
            res = append(res, temp)
            fmt.Println(res)
        }
    }

    return res
}

Output

0
[0]
[[] [0]]
1
[1]
[[] [0] [1]]
1
[0 1]
[[] [0] [1] [0 1]]
2
[2]
[[] [0] [1] [0 1] [2]]
2
[0 2]
[[] [0] [1] [0 1] [2] [0 2]]
2
[1 2]
[[] [0] [1] [0 1] [2] [0 2] [1 2]]
2
[0 1 2]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2]]
3
[3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3]]
3
[0 3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3]]
3
[1 3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3]]
3
[0 1 3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3]]
3
[2 3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3]]
3
[0 2 3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3]]
3
[1 2 3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3]]
3
[0 1 2 3]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 3]]
4
[4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 3] [4]]
4
[0 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 3] [4] [0 4]]
4
[1 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 3] [4] [0 4] [1 4]]
4
[0 1 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 3] [4] [0 4] [1 4] [0 1 4]]
4
[2 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 3] [4] [0 4] [1 4] [0 1 4] [2 4]]
4
[0 2 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 3] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4]]
4
[1 2 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] **[0 1 2 3]** [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4]]
4
[0 1 2 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] **[0 1 2 4]** [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4]]
4
[3 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4]]
4
[0 3 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4]]
4
[1 3 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4] [1 3 4]]
4
[0 1 3 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4] [1 3 4] [0 1 3 4]]
4
[2 3 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4] [1 3 4] [0 1 3 4] [2 3 4]]
4
[0 2 3 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4] [1 3 4] [0 1 3 4] [2 3 4] [0 2 3 4]]
4
[1 2 3 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4] [1 3 4] [0 1 3 4] [2 3 4] [0 2 3 4] [1 2 3 4]]
4
[0 1 2 4 4]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4] [1 3 4] [0 1 3 4] [2 3 4] [0 2 3 4] [1 2 3 4] [0 1 2 4 4]]
[[] [0] [1] [0 1] [2] [0 2] [1 2] [0 1 2] [3] [0 3] [1 3] [0 1 3] [2 3] [0 2 3] [1 2 3] [0 1 2 4] [4] [0 4] [1 4] [0 1 4] [2 4] [0 2 4] [1 2 4] [0 1 2 4] [3 4] [0 3 4] [1 3 4] [0 1 3 4] [2 3 4] [0 2 3 4] [1 2 3 4] [0 1 2 4 4]]

When it add the [0 1 2 4] element, it also change the existing [0 1 2 3] to [0 1 2 4]. Is there any specific reason why this happen?

  • 写回答

1条回答 默认 最新

  • dongzheng7165 2017-12-06 02:37
    关注

    In your append function calls you are using slices meaning you work with references, thus no guarantee that the values will remain static as @zerkms mentioned. What you can do is create a copy of the slice temp returned from the 1st append call and use it in the 2nd append. This will make passing a copy of the temp slice and with that avoiding any interferences.

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        fmt.Println(subsets([]int{0, 1, 2, 3, 4}))
    }
    
    func subsets(nums []int) [][]int {
        res := [][]int{}
    
        res = append(res, []int{})
        for i := 0; i < len(nums); i++ {
            for j := range res {
                fmt.Println(i)
                temp := append(res[j], nums[i])
                copy_temp := make([]int, len(temp))
                copy(copy_temp, temp)
                fmt.Println(copy_temp)
                res = append(res, copy_temp)
                fmt.Println(res)
            }
        }
    
        return res
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示