加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Pascal's Triangle II--LeetCode

发布时间:2020-12-13 20:06:54 所属栏目:PHP教程 来源:网络整理
导读:题目: Given an index k ,return the k th row of the Pascal's triangle. For example,given k = 3, Return [1,3,1] . Note: Could you optimize your algorithm to use only O ( k ) extra space? 思路:只用1个向量作为终究结果的存储空间便可,中间1直
 

题目:

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

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

Note:
Could you optimize your algorithm to use only O(k) extra space?

思路:只用1个向量作为终究结果的存储空间便可,中间1直使用这个向量来更新。

//第k层的数 void PascaltriangleKth(int k) { vector<int> result(k,0); int tmp1,tmp; for(int i=0;i<k;i++) { for(int j=0;j<=i;j++) { if(j==0 || j==i) { tmp = 0; tmp1 = 1; result[j]=1; } else { tmp = result[j]; result[j] += tmp1; tmp1 = tmp; } } } for(int j=0;j<result.size();j++) cout<<result[j]<<" "; cout<<endl; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读