leetcode 119. Pascal's Triangle II
2017-09-12 14:20
351 查看
原题:
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Note:
Could you optimize your algorithm to use only O(k) extra space?
输出杨辉三角的某一行的值。
代码如下:
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getRow(int rowIndex, int* returnSize) {
*returnSize=rowIndex+1;
int** temp;
temp=(int**)malloc(sizeof(int*)*(rowIndex+1));
for(int n=0;n<rowIndex+1;n++)
{
*(temp+n)=(int*)malloc(sizeof(int)*(n+1));
for(int m=0;m<=n;m++)
{
if(m==0||m==n)
{
temp
[m]=1;
}
else
{
temp
[m]=temp[n-1][m-1]+temp[n-1][m];
}
}
}
return *(temp+rowIndex);
}
一行一行算算就好咯。
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
[1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
输出杨辉三角的某一行的值。
代码如下:
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getRow(int rowIndex, int* returnSize) {
*returnSize=rowIndex+1;
int** temp;
temp=(int**)malloc(sizeof(int*)*(rowIndex+1));
for(int n=0;n<rowIndex+1;n++)
{
*(temp+n)=(int*)malloc(sizeof(int)*(n+1));
for(int m=0;m<=n;m++)
{
if(m==0||m==n)
{
temp
[m]=1;
}
else
{
temp
[m]=temp[n-1][m-1]+temp[n-1][m];
}
}
}
return *(temp+rowIndex);
}
一行一行算算就好咯。
相关文章推荐
- 119. Pascal's Triangle II LeetCode
- [Leetcode 118 and 119, Easy] Pascal's Triangle (I and II)
- Leetcode题解 119. Pascal's Triangle II
- [Leetcode]#119 Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- [LeetCode]119. Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- leetcode 119. Pascal's Triangle II 杨辉三角形2
- LeetCode 119. Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- Leetcode#119. Pascal's Triangle II(杨辉三角II)
- [leetCode 118 & 119] Pascal's Triangle I && II (杨辉三角问题)
- [LeetCode By Python]119. Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- Leetcode 119 Pascal's Triangle II
- 【Leetcode】119. Pascal's Triangle II
- leetcode119. Pascal's Triangle II
- 【LeetCode】119. Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- leetcode: (119) Pascal's Triangle II