119. Pascal's Triangle II
2015-08-12 15:15
337 查看
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Note:
Could you optimize your algorithm to use only O(k) extra space?
分析:每次计算只和上一层有关系,因此只需要一个数组空间就可以
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) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector<int> res(rowIndex+1,1); if(rowIndex == 0)return res; for(int i = 1; i <= rowIndex; i++) { int tmp = res[0]; for(int j = 1; j <= i-1; j++) { int kk = res[j]; res[j] = tmp+res[j]; tmp = kk; } } return res; } };
相关文章推荐
- 批处理执行多个SQL文件到oracle
- jquery中get传输方法实现读取xml文件
- pl/sql developer 编码格式设置
- storm记录--8-- Storm基本API
- ios OpenCv的配置和人脸识别技术
- 智能电视机顶盒开发记录
- 获得帮助
- Intent启动另一个App的三种方法
- TextWatcher的使用 ,实现一个显示剩余输入字数功能,分享工具类CalculateUtil
- 设计模式遵循的原则
- Internationalization and Locale
- 118. Pascal's Triangle
- 运用JS设置cookie、读取cookie、删除cookie
- tableView中cell的重用机制
- 【黑客】常抓肉鸡的几种方法初学必备-转发
- 获取访问网站客户ip地址
- 最短路
- leetcode 129 —— Sum Root to Leaf Numbers
- iOS 如何用post方式上传json数据
- 【Android架构GPS篇】之定位数据如何从GPS芯片到应用层(一)