Leetcode - Pascal's Triangle
https://leetcode.com/problems/pascals-triangle/
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
func generate(numRows int) [][]int { result := [][]int{} for i := 0; i < numRows; i++ { row := make([]int, i+1) for j := 0; j < len(row); j++ { row[j] = 1 } if len(result) == 0 { result = append(result, row) continue } last := result[len(result)-1] for j := 1; j < len(last); j++ { row[j] = last[j-1] + last[j] } result = append(result, row) } return result }