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. 10-排序5 PAT Judge (25 分)

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  2. JDK Integer

    1. public static int parseInt(String s, int radix) a. 充分考虑各种异常情况:字符串为空,带符号,进制出界,计算值出界 b. 计算时转换为负数进行处 ...

  3. Spark2.3.0 报 io.netty.buffer.PooledByteBufAllocator.metric

    Spark2.3.0依赖的netty-all-4.1.17.Final.jar 与 hbase1.2.0依赖的netty-all-4.0.23.Final.jar 冲突 <!-- Spark2. ...

  4. SQL SERVER – Configuration Manager – Cannot Connect to WMI Provider. You Do Not Have Permission or The Server is Unreachable

    打开SQL SERVER Configuarion Manger 出现以下错误 SQL Server Configuration Manager—————————Cannot connect to W ...

  5. my.宠物升级79级

    1.蚌仙子 74级半不到的时候,吃  月华露(500000经验的那种)  吃3个 正好 79级半 我记得 之前 是升满75级 再吃月华露 到79级的时候 经验溢出了.不突破还好,突破的话 宠物就升级了 ...

  6. formValidation校验

    引用: https://www.cnblogs.com/aliger/p/3898216.html

  7. (转)awk数组详解及企业实战案例

    awk数组详解及企业实战案例 原文:http://www.cnblogs.com/hackerer/p/5365967.html#_label03.打印数组:1. [root@nfs-server t ...

  8. (转)Nagios 配置及监控

    Nagios 配置及监控 原文:http://blog.csdn.net/linuxlsq/article/details/52606824 Nagios 监控 在互联网日益发展的今天,监控的重要性已 ...

  9. (转)shell解析命令行的过程以及eval命令

    shell解析命令行的过程以及eval命令   本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过 ...

  10. 【计算机网络】一步一步学习IP路由流程

    TCP/IP协议簇是目前互联网应用最广的协议栈,谈到TCP/IP协议栈就不能不讲一讲IP路由的问题,因为在我们使用的网络通信中几乎每时每刻都在发生着IP路由的事件…….当你在网络世界中还是一位新手的时 ...