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. node js linux / OS 安装

    rm -rf 删除文件夹名字rm -rf 软连接名称 1.安装taryum install -y tar 3. 下载node https://nodejs.org/en/download/ 4. 拷贝 ...

  2. Go语言基础之8--面向对象编程1之结构体(struct)

    一.结构体详解 1.1 声明和定义 1.Go中面向对象是通过struct来实现的, struct是用户自定义的类型 2.Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数 ...

  3. Go语言基础之6--值类型和引用类型

    一. 引用类型 引用类型理解为(C语言):指针 Golang中只有三种引用类型:slice(切片).map(字典).channel(管道): 实例1-1 package main import &qu ...

  4. 基本数据类型 list and tuple 04

    列表和元组 一,列表 1.列表 由[]括起来 可以存放各种数据类型:  存放量比较大 2.列表的索引和切片  列表也有索引 lst [i] i 即列表中各元素的位置 2.1列表的切片 lst[star ...

  5. canvas基础入门(二)绘制线条、三角形、七巧板

    复杂的内容都是有简单的线条结合而成的,想要绘制出复杂好看的内容先从画直线开始 canvas绘制直线先认识几个函数 beginPath():开始一条路径,或重置当前的路径 moveTo(x,y):用于规 ...

  6. myEclipse更换SVN登录账号

    Win10下 找到下面路径删除其下的所有文件 C:\Users\Administrator\AppData\Roaming\Subversion\auth\svn.simple AppData默认隐藏 ...

  7. html-3

    <hr> 下划线实体:想在页面显示被浏览器解析的内容为表格添加标题<caption>跟tr同级,只在<table>下 <link> 为页面加小图标 在& ...

  8. VSCode个人实用插件

    1.汉化插件 Chinese (Simplified) Language Pack for Visual Studio Code 发布者:Microsoft 2.主题插件(IDEA主题) Darcul ...

  9. Tree UVA - 548(二叉树递归遍历)

    题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...

  10. 3d Max 2012安装失败怎样卸载3dsmax?错误提示某些产品无法安装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...