LEETCODE--Pascal's Triangle II
来源:互联网 发布:手机简单制图软件 编辑:程序博客网 时间:2024/06/09 19:44
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?
Subscribe to see which companies asked this question
方法一:
从后往前进行加法赋值;
class Solution {public: vector<int> getRow(int rowIndex) { vector<int> A; A.push_back(1); if(rowIndex == 0) return A; A.push_back(1); if(rowIndex == 1) return A; for(int x = 2; x <= rowIndex; x++){ A.push_back(0); } for(int i = 2; i <= rowIndex; i++){ A[i] = 1; for(int j = i-1; j > 0; j--){ A[j] = A[j] + A[j-1]; } A[0] = 1; } return A; }};
方法二:
class Solution {public: vector<int> getRow(int rowIndex) { vector<vector<int>> finsh; vector<int> last; vector<int> add; finsh.push_back(add); finsh[0].push_back(1); rowIndex += 1; if(rowIndex == 1) return finsh[0]; else{ for(int i = 1; i < rowIndex; i++){ if(i == finsh.size()) { vector<int> add; finsh.push_back(add); } finsh[i].push_back(1); for(int j = 1; j <= i; j++){ if(finsh[i].size() == i) finsh[i].push_back(1); else{ int x = finsh[i-1][j-1]; int y = finsh[i-1][j]; int elem = x + y; finsh[i].push_back(elem); } } } return finsh[rowIndex-1]; } }};
0 0
- LeetCode Pascal's Triangle II
- LeetCode: Pascal's Triangle II
- [LeetCode] Pascal's Triangle II
- [Leetcode] Pascal's Triangle II
- 【leetcode】Pascal's Triangle II
- LeetCode - Pascal's Triangle II
- Leetcode: Pascal's Triangle II
- LeetCode-Pascal's Triangle II
- [leetcode] Pascal's Triangle II
- LeetCode - Pascal's Triangle II
- LeetCode | Pascal's Triangle II
- Pascal's Triangle II -leetcode
- Leetcode Pascal's Triangle II
- [LeetCode] Pascal's Triangle II
- Leetcode: Pascal's Triangle II
- Leetcode Pascal's Triangle II
- LeetCode Pascal's Triangle II
- LeetCode Pascal's Triangle II
- 分布式存储系统基本概念
- paint.setTypeface(Typeface.DEFAULT_BOLD);
- c++分数类
- js中的特有语句with和 for in
- 关于c#webform 通用变量的几种办法
- LEETCODE--Pascal's Triangle II
- Token是什么玩意
- Opencv 人脸识别
- HibernateTemplate、HibernateDaoSupport两种方法实现增删改查
- Unreal Engine 4 空场景创建 并为物体添加 Collision Volumes
- VMWare12安装OSX10.10
- activemq的多个broker之间的消息共享
- 关于css缓存的问题
- 苦逼码农们,这都是你咎由自取