leetcode124,Leetcode: Pascal's Triangle II

 2023-11-18 阅读 31 评论 0

摘要: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? 分析:細節實現題。代碼如下: class Solution { public:vector&

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?

分析:細節實現題。代碼如下:

class Solution {
public:vector<int> getRow(int rowIndex) {if(rowIndex == 0) return vector<int>(1,1);vector<int> result(2,1);for(int i = 3; i <= rowIndex+1; i++){vector<int> next;next.push_back(1);for(int j = 1; j < i-1; j++){next.push_back(result[j-1] + result[j]);}next.push_back(1);result = next;}return result;}
};

leetcode124,?

轉載于:https://www.cnblogs.com/Kai-Xing/p/4147097.html

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

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

发表评论:

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

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

底部版权信息