Pascal's Triangle II 解答
Question
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Solution
Similar with Pascal's Triangle. Note that the index starts from 0.
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>();
List<Integer> prev = new ArrayList<Integer>();
if (rowIndex < 0)
return result;
result.add(1);
prev.add(1);
while (rowIndex > 0) {
result = new ArrayList<Integer>();
result.add(1);
int length = prev.size();
for (int i = 0; i < length - 1; i++)
result.add(prev.get(i) + prev.get(i + 1));
result.add(1);
prev = result;
rowIndex--;
}
return result;
}
}
We can also only use one list.
public List<Integer> getRow(int rowIndex) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (rowIndex < 0)
return result;
result.add(1);
for (int i = 1; i <= rowIndex; i++) {
for (int j = result.size() - 2; j >= 0; j--) {
result.set(j + 1, result.get(j) + result.get(j + 1));
}
result.add(1);
}
return result;
}
Pascal's Triangle II 解答的更多相关文章
- 28. Triangle && Pascal's Triangle && Pascal's Triangle II
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II
118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- Pascal's Triangle,Pascal's Triangle II
一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...
- 杨辉三角形II(Pascal's Triangle II)
杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...
- LeetCode OJ 119. 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, ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- LeetCode Pascal's Triangle && Pascal's Triangle II Python
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
随机推荐
- Womany女人迷 | 氪加
Womany女人迷 | 氪加 Womany女人迷
- 浅谈C++调用C#的DLL程序方法
把C#编译成DLL或者Axtive控件,再由C调用!比如使用C++调用C#的DLL. SwfDotNet是.net下输出flash的类库.SwfDotNet是C#编写的,作者的C#水平,真是令我佩服. ...
- PHP初识
1. php是一种跨平台的语言,支持几乎全部的数据库. 以前觉得PHP与MYSQL是黄金组合,对于PHP能否支持MSSQL没有过了解,PHP支持几乎全部的数据库,也支持MSSQL(5.2.X版本可以用 ...
- CSS的优先级
样式的优先级: (内联样式表[嵌入式样式])>(内部样式表)>(外部样式表) 经过测试动手测试发现有个(唯一的)例外 情况:当引用外部样式在内部样式表(非嵌入式样式)的后面时,外部样式会覆 ...
- Git入门——基础知识问答
问题一:为什么要选择Git作为Android开发的版本控制工具? 答:1)git是android项目和社区的统一语言. 2)高通版本发布频繁,需要与平台及时同步,快速re ...
- 【转载】NSURLSession教程
原文:http://www.raywenderlich.com/51127/nsurlsession-tutorial 查理·富尔顿 2013年10月9日, 推特 注意从雷 :这是一个缩写版的一章 i ...
- TexturePacker文件的反向解析-TextureUnpacker
最近在使用cocos2d-x做开发,其中会用到TexturePacker工具打包纹理文件,但是有时候想从打包好的.plist和.png大图文件反向生成原始的小图文件,TexturePacker好像没有 ...
- codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Weights Xenia has a set of weig ...
- (译+原)std::shared_ptr及std::unique_ptr使用数组
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462363.html 参考网址: http://stackoverflow.com/questions ...
- Draw2d中的布局管理器Layout比较
最近在研究Eclipse中的GEF开发,在跟着GEF-whole-upload教程做一个GEF应用程序的例子时,发现Figure上的控件无法显示,谷歌了很久也没找到解决方案,最后终于发现是Layout ...