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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?