zoukankan      html  css  js  c++  java
  • Pascal's Triangle II <leetcode>

    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 };
  • 相关阅读:
    P1522 牛的旅行
    P1908 逆序对
    P1107 雷涛的小猫
    欧拉函数
    P2679 子串
    P1063 能量项链
    P1052 过河
    P1020 导弹拦截
    P1330 阳光封锁大学
    P1198 最大数
  • 原文地址:https://www.cnblogs.com/sqxw/p/3962282.html
Copyright © 2011-2022 走看看