问题描述:

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. 题解——loj6279 数列分块入门3 (分块)

    用set维护有序序列 或许sort也可以,但这题的前驱定义是严格小于 所以要去重 然后就是记得自己打的加法tag在query的时候一定要算上 话说这题数据有点fake啊忘了查询算上自己的标记了还有70 ...

  2. (转)Awesome Knowledge Distillation

    Awesome Knowledge Distillation 2018-07-19 10:38:40  Reference:https://github.com/dkozlov/awesome-kno ...

  3. GCN code parsing

    GCN code parsing 2018-07-18 20:39:11 utils.py  --- load data  def load_data(path="../data/cora/ ...

  4. 解决: docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest

    直接获取 rpm文件 wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.1 ...

  5. Images之管理image

    Manage images The easiest way to make your images available for use by others inside or outside your ...

  6. Kylin介绍2

    原理 官网 doc cube介绍 安装 案例 企业级特性 Apache Kylin 1.5的新功能和架构改变 Java  API 通过java代码对kylin进行cube build kylin从入门 ...

  7. Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树

    D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...

  8. 设计模式之组合模式(composite)

    概念: 将对象组合成树形结构以表示“部分-整体”的层次结构.使用户对单个对象和组合对象的使用更具有一致性. 适用性:想表示对象的部分-整体层次结构.

  9. vlookup+match高亮显示行

    VLOOPUP =VLOOKUP("*"&O3,$A$2:$B$38,2,0) 第一个参数查找值,第二个参数查找范围,第三个参数返回第几列的值,第四个参数匹配方式(“,”, ...

  10. vue生命周期钩子

    转载自:https://segmentfault.com/a/1190000008010666?utm_source=tag-newest https://segmentfault.com/a/119 ...