duanke2012 2018-05-21 03:19
浏览 21
已采纳

动态将切片作为行添加到2D切片

I'm trying to add the slice sofar to a new row in matrix after each iteration.

func combinations(sofar []int, rest []int, n int, matrix [][]int, count int) {
    if n == 0 {
        //Next two lines problematic
        matrix[count] = append(matrix[count], sofar[0], sofar[1], sofar[2])
        count++
        fmt.Println(sofar)
        } else {
            for i := range rest[:len(rest)] {
                concat := sofar
                concat = append(concat, rest[i])
                combinations(concat, rest[i+1:], n-1, matrix, count)
            }
        }
}

func factorial(x uint) uint {
    if x == 0 {
        return 1
    }
    return x * factorial(x-1)
}

Driver program

func triangleNumber() int {
    sofar := []int{}
    rest := []int{1,2,3,4}
    matrixSize := factorial(4)/(factorial(1)*factorial(3))
    matrix := make([][]int, matrixSize)
    count := 0 
    fmt.Println(matrixSize)
    combinations(sofar, rest, 3, matrix, count)
    return 0
}

triangleNumber()

I want matrix to be;

[1 2 3]
[1 2 4]
[1 3 4]
[2 3 4]

But instead it's all going in the first row. Also is there a way I can get rid of count, and just add the slice sofar to the next row?

  • 写回答

2条回答 默认 最新

  • dongreng9864 2018-05-21 04:26
    关注

    Actually, there are a couple of things I note with your program:

    1. Append adds to the existing slice at its end (after length), so if you are using append for matrix, you need not allocate a slice of that size (see main in the code below)
    2. After you are adding elements to the matrix, it is simply being dumped in your current program. The combinations function needs to return it back so that when future elements (slice of ints) are added, they are actually all there.

    I've added some debugs and remodeled your program a bit, see if it makes sense:

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        triangleNumber()
    }
    func combinations(sofar []int, rest []int, n int, matrix [][]int, count int) [][]int {
        fmt.Println("Entered with matrix", matrix)
        if n == 0 {
            fmt.Println("Entered with count", count)
            //Next two lines problematic
            matrix = append(matrix, sofar)
            count++
            fmt.Println(sofar)
            fmt.Println("Printing matrix
    ***", matrix, "
    ***")
            return matrix
        } else {
            for i := range rest[:len(rest)] {
                concat := sofar
                concat = append(concat, rest[i])
                matrix = combinations(concat, rest[i+1:], n-1, matrix, count)
                fmt.Println("Sending with count", count)
            }
        }
        return matrix
    }
    
    func factorial(x uint) uint {
        if x == 0 {
            return 1
        }
        return x * factorial(x-1)
    }
    
    func triangleNumber() int {
        sofar := []int{}
        rest := []int{1, 2, 3, 4}
        matrixSize := factorial(4) / (factorial(1) * factorial(3))
        matrix := make([][]int, 0)
        count := 0
        fmt.Println(matrixSize)
        combinations(sofar, rest, 3, matrix, count)
        return 0
    }
    

    And as you can see, you can pretty much get rid of count too with this approach (look at the output). There's still some scope for improvement left though, but I guess this addresses what you were asking.

    On playground: https://play.golang.org/p/rnCdPcaIG3N

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)