[LeetCode65]Pascal's Triangle II
来源:互联网 发布:window10不能安装软件 编辑:程序博客网 时间:2024/06/16 19:49
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?
Analysis:
滚动数组实现.
C++
vector<int> getRow(int rowIndex) { vector<int> result(rowIndex+2,0); result[1]=1; for(int i=0;i<rowIndex;i++){ for(int j=rowIndex+1;j>0;j--){ result[j]= result[j-1]+result[j]; } } result.erase(result.begin()); return result; }java
public List<Integer> getRow(int rowIndex) { List<Integer> result = new ArrayList<>(); for(int i=0;i<rowIndex+2;i++) result.add(0); result.set(1,1); for(int i=0;i<rowIndex;i++){ for(int j=rowIndex+1;j>0;j--) result.set(j, result.get(j-1)+result.get(j)); } result.remove(0); return result; }
Java
0 0
- [LeetCode65]Pascal's Triangle II
- Pascal's Triangle、Pascal's Triangle II
- Pascal's Triangle , Pascal's Triangle II
- Pascal's Triangle & Pascal's Triangle II
- Pascal's Triangle && Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle II
- Android 拷贝数据库文件
- 怎么做才是好的IT榜样?
- 黑马程序员_OC学习之@property、点语法
- C++实现TCP通信。。
- java的class文件反编译和Eclipse、MyEclipse反编译插件安装、使用
- [LeetCode65]Pascal's Triangle II
- jsp速度优化七种方法
- android FragmentManage FragmentTransaction介绍
- 程序员自我提高的几点建议
- WebService 之 WSDL文件 讲解
- 黑马程序员--Java常用的类
- android 简析 addToBackStack使用和Fragment执行流程
- 局部变量
- 使用“swap技巧”除去多余的容量