# Pascal's Triangle

• public class YangHui
{
public static void main(String args[])
{
int x=10;//输入几层是几层
int num[][] = new int[x][x];//这个数组有几层
for(int m=0;m<x;m++)//主要是对数组进行赋值
{
for(int n=0;n<=m;n++)//每一层的个数都是小于等于层数的，m代表层数，n代表着第几个数
{
if(n==0||m==n)//每一层的开头都是1，m==n的时候也是1,必须要这个，凡事都得有个开头
{
num[m][n]=1;
}
else
num[m][n]=num[m-1][n-1]+num[m-1][n];//这个就是递推的方法了，例如3=1+2，3的坐标就是3[3,1]=1[2,0]+2[2,1];
System.out.print(num[m][n]+" ");
}
System.out.println();
}
}
}

• -(NSArray *)pascalsTrianglewithnumRows:(int)numRows{

``````NSMutableArray *triangleArr = [NSMutableArray new];

if (numRows == 0 ) {
return triangleArr;
}

for (int i = 1; i<numRows; i++) {

NSMutableArray *rowArr = [NSMutableArray new];

NSArray *preRowArr = [NSArray new];
preRowArr = triangleArr[i -1];

for (int j = 1; j<i; j++) {
int sum =  [preRowArr[j-1] intValue] +  [preRowArr[j] intValue];
}

}
return triangleArr;
``````

}

• '''
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows == 0:
return []
elif numRows == 1:
return [[1]]
elif numRows >1:
tri = [1]
res = [[1]]
for i in range(2, numRows+1):
tria = [0] +tri + [0]
tri = [sum(tria[j:j+2]) for j in range(len(tria)-1)]
res.append(tri)
return res
'''

• class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows == 0:
return []
elif numRows == 1:
return [[1]]
elif numRows >1:
tri = [1]
res = [[1]]
for i in range(2, numRows+1):
tria = [0] +tri + [0]
tri = [sum(tria[j:j+2]) for j in range(len(tria)-1)]
res.append(tri)
return res

• A very simple solution in Python
class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
result = []
L = [1]
i = 0
if numRows == 0: return result
while i < numRows:
result.append(L)
L = [x + y for x, y in zip([0] + L, L + [0])]
i += 1
return result

• # Binomial Coefficient 40 ms runtime beats 91.08% of Python 3 submissions.

``````class Solution:
def factorial(self, n):
if n in [0, 1]:
return 1
return n * Solution.factorial(self, n - 1)

def getRow(self, n):
"""
:type rowIndex: int
:rtype: List[int]
"""
row = []
for k in range(n + 1):
row.append(self.factorial(n) // (self.factorial(k) * self.factorial(n - k)))
return row

def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
pascale_triangle = []
for i in range(0, numRows):
pascale_triangle.append(self.getRow(i))
return pascale_triangle
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.