码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 119 Pascal's Triangle II

时间:2018-07-16 11:15:19      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:question   interview   技术分享   anim   class   only   index   cto   rect   

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal‘s triangle.

Note that the row index starts from 0.

技术分享图片
In Pascal‘s triangle, each number is the sum of the two numbers directly above it.

Example:


Follow up:

 

c++:

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        
         vector<vector<int> > triangle;
          int j=1;
          for(int i=0;i<=rowIndex;i++)
          {
              vector<int> a;
              for(int k=0;k<j;k++)
              {
                  if(k==0||k==j-1) 
                      a.push_back(1);
                  else
                      a.push_back(triangle[i-1][k]+triangle[i-1][k-1]);
                  
              }
              j++;
              triangle.push_back(a);
          }
        return triangle[rowIndex];
        
    }
};

 

Could you optimize your algorithm to use only O(k) extra space?


 
 

LeetCode 119 Pascal's Triangle II

标签:question   interview   技术分享   anim   class   only   index   cto   rect   

原文地址:https://www.cnblogs.com/dacc123/p/9315897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!