website/content.en/ChapterFour/0100~0199/0118.Pascals-Triangle.md
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
Note: 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]
]
Given a non-negative integer numRows, generate the first numRows rows of Pascal's triangle. In Pascal's triangle, each number is the sum of the numbers to its upper left and upper right.
package leetcode
func generate(numRows int) [][]int {
result := [][]int{}
for i := 0; i < numRows; i++ {
row := []int{}
for j := 0; j < i+1; j++ {
if j == 0 || j == i {
row = append(row, 1)
} else if i > 1 {
row = append(row, result[i-1][j-1]+result[i-1][j])
}
}
result = append(result, row)
}
return result
}