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 }

 

转载于:https://www.cnblogs.com/dreamrun/p/4564723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值