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 解答的更多相关文章

  1. 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 ...

  2. 【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, ...

  3. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

  4. 杨辉三角形II(Pascal's Triangle II)

    杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...

  5. 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, ...

  6. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  7. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

  8. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  9. 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 ...

随机推荐

  1. STL中istream_iterator和ostream_iterator的基本用法

    标准程序库定义有供输入及输出用的iostream iterator类,称为istream_iterator和ostream_iterator,分别支持单一型别的元素读取和写入.使用这两个iterato ...

  2. 第06讲- DDMS中logcat的使用

    1.DDMS使用 )Device选项卡 Device中罗列了Emulator中所有的进程,选项卡右上角那一排按钮分别为:调试进程.更新进程.更新进程堆栈信息.停止某个进程. )Threads选项卡   ...

  3. MSDN中HttpWebRequest/HttpWebResponse用法

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://home.cnblogs.com/u/weiweiboqi/ ...

  4. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  5. JQuery 1.3.2联动获取部门

    Sql       $(document).ready(function(){ $(".dept").bind("click", function () { v ...

  6. MySql命令——函数

    1.拼接字段——Concata() 把多个串连接起来形成一个较长的串. select concat(value,'(',id,')') from test; 2.去掉空格 RTrim() 去掉右边的空 ...

  7. ionic 图片轮播问题

    1.使用ion-slide可以实现图片轮播,但是如果在html中仅仅增加ion-slide是远远不够的,会出现两个问题: (注:使用的是angularjs.首先需要在,js文件中注入:$ionicSl ...

  8. 怎么使用jQuery在DIV适应屏幕大小一直居中

    js的代码是这样的: $(function(){ $(window).resize(function(){ $(".login").css({ position: "ab ...

  9. VML :Vector Markup Language

    在以前老是浏览器IE<9在不支持SVG情况下,IE一般通过VML来绘制图形,图片,文字等 步骤: 必须在头部添加 <HTML xmlns:v="urn:schemas-micro ...

  10. 使用C#创建自定义背景色/形状的菜单栏与工具栏

    C#对于菜单栏与工具栏都提供了统一的背景色,形状的渲染类,即ToolStripRenderer类,同时根据不同的情形,提供了多个继承类,分别是ToolStripProfessionalRender,T ...