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. haproxy安装:

    haproxy安装: tar zxf haproxy-1.4.24.tar.gz            ##解压 yum install rpm-build -y                   ...

  2. http协议&接口规范&接口测试入门

    http协议 请求: 请求行:请求方法.url(协议名://ip;端口/工程名/资源路径).协议版本 请求头 :键值对 请求正文 响应: 响应行:协议版本.响应状态码.响应状态码描述 响应头 :键值对 ...

  3. 阿里云ECS服务器FileZilla'被动模式失败'的处理办法

    现象:FileZilla客户端连接服务器报错:“状态:    服务器发回了不可路由的地址.被动模式失败.” 解决办法:1.在[FileZilla Server]-Edit-Settings-Passi ...

  4. jquery插件分页

    收藏地址: http://www.jq22.com/yanshi5697

  5. my05_mysql检查点简述

    简单描述一下mysql 检查点,对mysql数据库恢复的理解有所帮助. 数据库版本 mysql> select version(); +-----------+ | version() | +- ...

  6. 《mac的git安装手册-1》

    <mac的git安装手册-1> 下载地址 https://git-scm.com/downloads 如果遇到上面这个问题打开系统偏好设置: OK,这样就能安装了

  7. 研磨设计模式学习笔记1--简单工厂(SimpleFactory)

    需求:实现一个简单工厂,客户端根据需求获取实现类. 简单工厂优点: 客户端不需要知道工厂内部实现,然组件外部实现面向接口编程. 客户端.实现类解耦. 一.接口及实现类 //接口 public inte ...

  8. 性能测试工具Jmeter08-Jmeter断言(检查点)

    断言是在请求的返回层面增加一层判断机制.因为请求成功了,并不代表结果一定正确,因此需要检测机制提高测试准确性. 下面介绍常用的jmeter三种断言 1.响应断言 例如: 模式匹配规则 2.Size A ...

  9. LeetCode 455.分发饼干(C++)

    假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j ,都有一个尺寸 ...

  10. CSS伪类:first-child与:first-of-type的异同

    CSS里关于元素匹配里面有两个非常类似却又不尽相同的选择器,伪类 :first-child 和 :first-of-type 两者在匹配方式上有很大差异,其实在一开始自己也没去注意这个细节,直到上次一 ...