Skip to content

Files

Latest commit

3fac9d5 · Feb 17, 2023

History

History

118 - Pascals Triangle

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 17, 2023
Feb 17, 2023

118. Pascals Triangle share

Problem Statement

Given an integer numRows, return the first numRows of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

 

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

Input: numRows = 1
Output: [[1]]

 

Constraints:

  • 1 <= numRows <= 30

Solutions

package main

func generate(numRows int) [][]int {
	pascal := make([][]int, numRows)
	for i := 0; i < numRows; i++ {
		row := make([]int, i+1)
		row[0] = 1
		row[i] = 1
		for j := 1; j < i; j++ {
			row[j] = pascal[i-1][j-1] + pascal[i-1][j]
		}
		pascal[i] = row
	}

	return pascal
}