Pescal Triangle Two
Description:
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?
Thoughts:
我们从前一个例子Pascal triangle的第二种方法可以得到启发;只需要去掉外面用来保存每一行List值的ArrayList即可。不过要注意的一个问题就是Pascal triangle中的rownums会比Pascal triangle two中的rowIndex多1,所以有以下的java代码:
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>();
rowIndex++;
for(int i=0;i<rowIndex;i++){
result.add(0, 1);
for(int j = 1;j<result.size()-1;j++){
result.set(j, result.get(j)+result.get(j+1));
}
}
return result;
}
}
Pescal Triangle Two的更多相关文章
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- Triangle - Delaunay Triangulator
Triangle - Delaunay Triangulator eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
随机推荐
- C语言--字符串和数字的相互转换
1.数字转换为字符串 sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出. sprintf 是个变参函数,定义如下: int sp ...
- UIEvent UIResponder UI_04
1.事件(UIEvent),是由硬件设备捕捉到用户对设备的操作,把这个操作抽象成一个事件对象 ios中三大事件:触Touches摸晃动事件Motion,远程控制事件RemoteControl: ...
- [Ext.Net]TreePanel+gridPanel实例
@小花要完整例子,尝试一下图文并茂,力求完整. gridPanel TreePanel.JPG (27.49 KB, 下载次数: 16) 下载附件 保存到相册 2013-1-6 11:24 上 ...
- Linux Shell -- 无网不利
这篇文章中我介绍几个非常实用的和网络相关的命令 一.ifconfig 这个命令在Windows下被"翻译为ipconfig",它用于显示网络接口,子网掩码等详细信息. 注:在每个系 ...
- (十二)UITableView的基本使用
UITableView之所以支持滚动,是因为继承自UIScrollView.默认是垂直滚动,性能极佳. UITableView的两种样式: 1.UITableViewStylePlain ...
- 供应商信息全SQL
SELECT hou.name, pv.vendor_name 供应商, pv.party_id, pvs.vendor_site_id, pvs.terms_id, pv.vendor_name_a ...
- 【一天一道LeetCode】#11Container With Most Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers a1, a2, -, an, where each represents a point at c ...
- Hadoop 的 TotalOrderPartitioner
Partition所处的位置 Partition位置 Partition主要作用就是将map的结果发送到相应的reduce.这就对partition有两个要求: 1)均衡负载,尽量的将工作均匀的分配给 ...
- 能量最小化初探,graphcuts能量最小化调用
1.相对于能量函数来说,能量最小化的办法都有哪些? 梯度下降 模拟退火 图割 2.这个 跟最优化问题的求解,有什么联系跟区别呢? 基本上差不多,其实就是求出来了函数的一个最小值,我们看问题的时候不妨把 ...
- android 自定义Viewpager实现无限循环
; i < imageUrls.length; i ++){ ADInfo info = new ADInfo(); info.setUrl(imageUrls[i]); info.setCon ...