LEETCODE,[LeetCode]119.Pascal's Triangle II

 2023-10-07 阅读 28 评论 0

摘要:版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/SunnyYoona/article/details/43562603 題目 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 yo
版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/SunnyYoona/article/details/43562603

題目

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?

LEETCODE。思路

代碼

    /**------------------------------------*   日期:2015-02-06*   作者:SJF0115*   題目: 119.Pascal's Triangle II*   網址:https://oj.leetcode.com/problems/pascals-triangle-ii/*   結果:AC*   來源:LeetCode*   博客:---------------------------------------**/#include <iostream>#include <vector>#include <algorithm>using namespace std;class Solution {public:vector<int> getRow(int rowIndex) {vector<int> row(rowIndex+1);vector<int> tmp = row;for (int i = 0;i < rowIndex+1;++i) {tmp[0] = tmp[i] = 1;for (int j = 1;j < i;++j) {tmp[j] = row[j-1] + row[j];}//forrow = tmp;}//forreturn row;}};int main(){Solution s;int n = 0;vector<int> result = s.getRow(n);// 輸出for(int i = 0;i < result.size();++i){cout<<result[i]<<"  ";}//forcout<<endl;return 0;}

運行時間

這里寫圖片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/123574.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息