[LeetCode] Pascal's Triangle II
来源:互联网 发布:淘宝如何注销实名认证 编辑:程序博客网 时间:2024/06/05 02:45
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) { vector<int> res(rowIndex + 1, 1); for(int i = 2; i <= rowIndex; i++) { vector<int> tmp(res); for(int j = 1; j < i; j++) tmp[j] = res[j] + res[j-1]; res = tmp; } return res; }};
better one:
class Solution {public: vector<int> getRow(int rowIndex) { vector<int> res(rowIndex + 1); res[0] = 1; for(int i = 1; i <= rowIndex; i++) { for(int j = i; j >= 0; j--) { if(j == i) res[j] = res[j-1]; else if(j > 0) res[j] = res[j] + res[j-1]; } } return res; }};
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
- 11782 - Optimal Cut(树形DP+记忆化搜索)
- java对文件的读写
- 多继承的时候如果两个父类中出现了同样的方法,而你要在子类调用此方法,调用时请加类限定符
- JavaScript跨域总结与解决办法
- 逆波兰式的实现(栈实现)
- [LeetCode] Pascal's Triangle II
- MySQL- SHOW TABLE STATUS命令
- Service-startService启动的服务(1)
- iOS 基础类解析 - NSObject
- 动态添加控件
- about <<Learning Modern 3D Graphics Programming>>
- 虚继承
- 80端口穿透软件之nat123端口映射原理分析及使用方法
- 在OS X 10.9.2安装gcc-4.9.0——关于gcc对go语言的支持