Pascal's Triangle

Solution in Go

func generate(numRows int) [][]int {
    var rows [][]int

    for i := 0; i < numRows; i++ {
        row := make([]int, i + 1)

        for j := 0; j <= i; j++ {
            if j == 0 || j == i {
                row[j] = 1
            } else {
                prevRow := rows[i - 1]
                row[j] = prevRow[j] + prevRow[j - 1]
            }
        }

        rows = append(rows, row)
    }

    return rows    
}
Subscribe via email

Get notified once/twice per month when new articles are published.

Copyright © 2022 - 2023 TheDeveloperCafe.
The Go gopher was designed by Renee French.