问题描述:

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. vs2015 + Python3.5 环境搭建

    1. vs2015只支持Python3.5及以前的版本,对应Anaconda3.4.2之前的版本. 2. 卸载掉所有安装过的Python 3. 建议重装VS2015, 因为增量升级Python Too ...

  2. ssm项目部署到服务器过程

    ssm项目部署到服务器过程 特别篇 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Sho ...

  3. How do I extract a single column from a data.frame as a data.frame

    Say I have a data.frame: df <- data.frame(A=c(10,20,30),B=c(11,22,33), C=c(111,222,333))  A  B  C ...

  4. 题解——CodeForces 438D The Child and Sequence

    题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...

  5. (转载)c# winform comboBox的常用一些属性和用法

    comboBox的常用一些属性和用法 [1].控件的默认值怎么设? this.comboBox1.Text = "请选择港口"; comboBox1.Items.Add(" ...

  6. Git回顾

    抄自廖雪峰的官方网站 完整图文请访问https://github.com/Mrlution/study/tree/master/git 关于repository 我认为repository是一个存放代 ...

  7. sublime text 中文显示异常

    本文链接:sublime text 中文显示异常 http://www.cnblogs.com/daysme/p/7549857.html 前言 sublime text 3143 用了一年时候,最近 ...

  8. 【译】第22节---Fluent API - EntityTypeConfiguration类

    原文:http://www.entityframeworktutorial.net/code-first/entitytypeconfiguration-class.aspx 在我们开始使用Fluen ...

  9. python线程 有问题?

  10. hdu-5707-Combine String

    题意:给你三个字符串,让你计算1 2 串和3 串是否匹配,就是3串可以分解为 1  2 串,字母顺序必须是按照1 2 串的字母前后顺序. DP代码太深奥 看不太透,这个代码比较好理解一点: #incl ...