# 118. Pascal's Triangle 杨辉三角

@TOC

[LeetCode]

Total Accepted: 83023 Total Submissions: 247907 Difficulty: Easy

## # 题目描述

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]
]
``````

## # 解题方法

### # Java解法

``````public class Solution {
public List<List<Integer>> generate(int numRows) {
if(numRows<=0)  return new ArrayList();
List<Integer> temp=new ArrayList();
for(int i=2;i<=numRows;i++){
temp=new ArrayList();
for(int j=0;j<i;j++){
if(j==0 || j==i-1){
}else{
}
}
}
}
}
``````

AC:2ms

### # Python解法

``````class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
res = [[1 for j in range(i)] for i in range(1, numRows + 1)]
for i in range(2, numRows):
for j in range(1, i):
res[i][j] = res[i - 1][j - 1] + res[i - 1][j]
return res
``````

## # 日期

2016 年 05月 8日 2018 年 11 月 19 日 —— 周一又开始了