Pascal’s Triangle
Given numRows, generate the first numRows of Pascal’s triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Array
简单
题目意思是给定一个行数,返回一个杨辉三角形。解题思路是除了每行的首尾的值为1,剩下的值为上一行的前后两个之和。
int** generate(int numRows, int** columnSizes)
{
int i=0;
int j=0;
if(numRows == 0)
return 0;
int ** returnArray = (int **)malloc(sizeof(int *) * numRows);
*columnSizes = (int *)malloc(sizeof(int)*numRows);
for(i=0; i<numRows; i++)
{
(*columnSizes)[i] = i+1;
returnArray[i] = (int *)malloc(sizeof(int) * (i+1));
for(j=0; j<i+1; j++)
{
if( (0 == j) || (i == j) )
returnArray[i][j] = 1;
else
returnArray[i][j] = returnArray[i-1][j-1] + returnArray[i-1][j];
}
}
return returnArray;
}