问题描述:

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?

问题分析:

第 rowIndex 行有 rowIndex + 1 个元素,给这 rowIndex + 1个元素赋初值 0,

row的第一个元素赋值为1,此时,row的内容为[1,0,0,0,......]

由第 j 行生成第j + 1行,初始为[1,0,0,0,......],逆序将相邻两个元素相加后,赋值给后者
j == 0 时,生成的是 [1,1,0,0,......]
j == 1 时,生成的是 [1,2,1,0,......]
......
j == rowIndex - 1,生成最终结果

代码:

 public List<Integer> getRow(int rowIndex) {
List<Integer> row = new ArrayList<Integer>();
if(rowIndex < 0) return row;
// 第 rowIndex 行有 rowIndex + 1 个元素,给这 rowIndex + 1个元素赋初值 0
for(int i = 0; i <= rowIndex; ++i){
row.add(0);
} row.set(0, 1); //生成第rowIndex行数据,最小为第0行
//由第 j 行逆序生成第j + 1行,初始为 1
//j == 0 时,生成的是 1 1
//j == 1 时,生成的是 1 2 1
//......
//j == rowIndex - 1,生成最终结果
for(int j = 0; j < rowIndex; ++j){
for(int k = rowIndex - 1; k > 0; --k){ //从最后一个元素开始,逆序计算
row.set(k, row.get(k) + row.get(k - 1));
}
}
row.set(rowIndex, 1); //给最后一位赋值1
return row;
}

Pascal's Triangle 2(leetcode java)的更多相关文章

  1. Pascal's Triangle II Leetcode java

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  2. Pascal's Triangle II —LeetCode

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. Pascal's Triangle II leetcode

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  4. LeetCode算法题-Pascal's Triangle II(Java实现)

    这是悦乐书的第171次更新,第173篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第30题(顺位题号是119).给定非负索引k,其中k≤33,返回Pascal三角形的第k ...

  5. Java for LeetCode 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. Java for LeetCode 118 Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  7. 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...

  8. [Leetcode][JAVA] Pascal's Triangle I, II

    Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...

  9. leetcode 119 Pascal's Triangle II ----- java

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

随机推荐

  1. P4980 【模板】Polya定理

    思路 polya定理的模板题,但是还要加一些优化 题目的答案就是 \[ \frac{\sum_{i=1}^n n^{gcd(i,n)}}{n} \] 考虑上方的式子怎么求 因为\(gcd(i,n)\) ...

  2. Optical Flow related Tutorials

    Optical Flow related Tutorials 2017-04-01 10:50:55 Reference: 1. http://blog.csdn.net/carson2005/art ...

  3. go 依赖工具glide

    添加gopath/bin目录到环境变量下 安装glide $ go get github.com/Masterminds/glide $ go install github.com/Mastermin ...

  4. facebook api之Access and Authentication

    Access and Authentication There are three access levels to the Marketing APIs. You can upgrade acces ...

  5. Latex: 解决 The gutter between columns is x inches wide (on page x), but should be at least 0.2 inches. 问题

    参考: Sample_WCCI.tex Latex: 解决 The gutter between columns is x inches wide (on page x), but should be ...

  6. 将 Graphviz .dot 文件转换为其他格式的图像

    参考: Graphviz: How to go from .dot to a graph? 将 Graphviz .dot 文件转换为其他格式的图像 在Linux系统下,使用以下命令: dot -Tp ...

  7. Broken Keyboard (a.k.a. Beiju Text) 思路

    问题:You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem ...

  8. SqlServer中常常搞不清楚 sp_columns来看一看

    The sp_columns catalog stored procedure is equivalent to SQLColumns in ODBC. The results returned ar ...

  9. STL_map.VC6简单使用例子

    1. #include <windows.h> //使用map时会出现如下警告:主要意思是 identifier was truncated to '255' characters in ...

  10. MVC路由 路由的三种扩展 替换MVC内置的Handler

    Global.asax 是 程序入口文件 路由配置   为什么localhost:8088/Home/Index/1 能返问到我们写的 会去掉前缀跟端口号  变成Home/Index/1 用这个跟路由 ...