zoukankan      html  css  js  c++  java
  • No.118 Pascal's Triangle ||

    No.118 Pascal's Triangle ||

    Given an index k, return the kth row of the Pascal's triangle.

    For example, given k = 3,
    Return [1,3,3,1].

    思路:

      生成帕斯卡三角形第rowIndex+1
      粗心大意,不认真读题!!!
      和生成帕斯卡三角形一样,不过不用保存中间过程,只返回最后一行即可

     1 #include "stdafx.h"
     2 #include <string>
     3 #include <vector>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 class Solution
     9 {
    10 public:
    11     vector<int> getRow(int rowIndex)
    12     {//生成帕斯卡三角形第rowIndex+1行
    13      //粗心大意,不认真读题!!!
    14      //和生成帕斯卡三角形一样,不过不用保存中间过程,只返回最后一行即可
    15         vector<int> res;
    16         if(rowIndex<0)
    17             return res;
    18         vector<int> front;
    19         for(int i=0; i<rowIndex+1; i++)
    20         {
    21             res.clear();
    22             for(int j=0; j<i+1; j++)
    23             {
    24                 if(j==0 || j==i)
    25                     res.push_back(1);
    26                 else
    27                 {
    28                     res.push_back(front[j]+front[j-1]);//又是i、j傻傻分不清楚!!!
    29                 }
    30             }
    31             front = res;
    32         }
    33         return res;
    34     }
    35 };
    36 
    37 int main()
    38 {
    39     Solution sol;
    40     int numRows;
    41     vector<int> res;
    42     while(cin >> numRows)
    43     {
    44         cout << numRows<<" : "<<endl;
    45         res = sol.getRow(numRows);
    46         for(const auto &i : res)
    47             cout << i << " ";
    48         cout << endl;
    49     }
    50 }
  • 相关阅读:
    ambari
    linux常用命令
    scala新版本学习(3)
    Spring中的AOP
    Spring中的Bean
    Spring的基本应用(1):依赖以及控制反转
    Scala新版本学习(2):
    python之time模块
    python之编码与解码
    爬虫之re数据提取的使用
  • 原文地址:https://www.cnblogs.com/dreamrun/p/4564723.html
Copyright © 2011-2022 走看看