标签:style blog color io strong for div sp 代码
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?
算法:根据帕斯卡三角形的规律直接求,比较简单,可能有其他好的方法,代码如下:
1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<int> temp(rowIndex+1); 5 vector<int> pre(rowIndex+1); 6 pre[0]=1; 7 for(int i=0;i<=rowIndex;i++) 8 { 9 temp[0]=1; 10 temp[i]=1; 11 for(int j=1;j<i;j++) 12 { 13 temp[j]=pre[j-1]+pre[j]; 14 } 15 pre=temp; 16 } 17 return temp; 18 } 19 };
Pascal's Triangle II <leetcode>
标签:style blog color io strong for div sp 代码
原文地址:http://www.cnblogs.com/sqxw/p/3962282.html