Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 1.49 KB

File metadata and controls

66 lines (48 loc) · 1.49 KB

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
}