Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 3
Output: [1,3,3,1]

Follow up:

Could you optimize your algorithm to use only O(k) extra space?

Solution: only using O(k), update the elements in the array, we need temp  to keep the previous element

using list to set : list.set(index, num);

class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
res.add(1);
for(int i = 1; i<=rowIndex; i++){
int temp = res.get(0);//previous element
for(int j = 1; j<i; j++){
int k = res.get(j);
res.set(j, k+temp);
temp = k;
}
res.add(1);
}
return res;
}
}

119. Pascal's Triangle II (Amazon) from leetcode的更多相关文章

  1. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

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

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

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

  4. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  5. 119. Pascal's Triangle II@python

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  6. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

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

  8. 【一天一道LeetCode】#119. Pascal's Triangle II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

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

随机推荐

  1. jq自定义鼠标右键菜单

    效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  2. Oracle的CLOB大数据字段类型

    转载:https://www.cnblogs.com/Grand-Jon/p/7389427.html 一.Oracle中的varchar2类型 我们在Oracle数据库存储的字符数据一般是用VARC ...

  3. Linux用户登录信息

    1.用户登录日志信息 /var/run/utmp:记录当前正在登录系统的用户信息,默认由who和w记录当前登录用户的信息,uptime记录系统启动时间: /var/log/wtmp:记录当前正在登录和 ...

  4. 1.5 GO json转Map

    使用GO将show slave status查询返回的json串转为Map类型 package main import ( "encoding/json" "fmt&qu ...

  5. java——数据结构

    底层数据结构: 数组 ArrayList 链表 LinkedList 应用数据结构: 二分搜索树 BST 最大堆/最小堆 MaxHeap/MinHeap 线段树 SegmentTree 字典树 Tri ...

  6. Dockerfile的书写规则和指令的使用方法

    Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令.Docker程序将这些Dockerfile指令翻译真正的Linux命 ...

  7. impdp参数TABLE_EXISTS_ACTION

    [转自:http://space.itpub.net/519536/viewspace-631445] 当使用IMPDP完成数据库导入时,如遇到表已存在时,Oracle提供给我们如下四种处理方式:a. ...

  8. Unable to instantiate receiver xxx.receiver.NetworkReceiver异常

    Unable to instantiate xxxreceiver.NetworkReceiver: 这个异常是之前版本有NetworkReceiver(监听网络变化的),新的版本删除了Network ...

  9. 电感的Q值

    电感的Q值又称为品质因数,即在通过一定频率信号时,感抗与等效损耗之比.品质因数越高即系统损耗越小效率越高,一般为50`100,最高500左右,再大就会烧毁.一般Q值与很多因素有关:绕线粗细,长度与直径 ...

  10. [转]Passing data between pages in JQuery Mobile mobile.changePage

    本文转自:http://ramkulkarni.com/blog/passing-data-between-pages-in-jquery-mobile/ I am working on a JQue ...