119. Pascal's Triangle II

Easy

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?

package leetcode.easy;

import java.util.ArrayList;
import java.util.List; public class PascalSTriangleII {
@org.junit.Test
public void test() {
System.out.println(getRow(3));
} public List<Integer> getRow(int rowIndex) {
List<Integer> row = new ArrayList<>();
row.add(1);
if (rowIndex == 0) {
return row;
}
for (int rowNum = 1; rowNum <= rowIndex; rowNum++) {
List<Integer> prevRow = row;
row = new ArrayList<>();
row.add(1);
for (int j = 1; j < rowNum; j++) {
row.add(prevRow.get(j - 1) + prevRow.get(j));
}
row.add(1);
}
return row;
}
}

LeetCode_119. Pascal's Triangle II的更多相关文章

  1. 28. Triangle && Pascal's Triangle && Pascal's Triangle II

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  2. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

  4. 杨辉三角形II(Pascal's Triangle II)

    杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...

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

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

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

  7. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

  8. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  9. LeetCode Pascal's Triangle && Pascal's Triangle II Python

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

随机推荐

  1. postgresql —— 查看索引

    查索引 语句: SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename = 'user_tbl' ORDER BY ...

  2. Python+request 使用pymysql连接数据库mysql的操作,基础篇《十一》

    笔记记录: (1)pymysql中所有的有关更新数据(insert,update,delete)的操作都需要commit,否则无法将数据提交到数据库,既然有了commit(),就一定有对应的rollb ...

  3. 微信小程序学习记录(一)

    如何定义一个全局变量: 1,在根目录下app.js中添加 App({ globalData: { g_isPlayingMusic : false, g_currentMusicPostId :nul ...

  4. 49、[源码]-Spring容器创建-创建Bean准备

    49.[源码]-Spring容器创建-创建Bean准备

  5. bzoj 3498

    统计三元环 很多代码在bzoj都T诶 #include <iostream> #include <cstdio> #include <algorithm> #inc ...

  6. syniverse是一家怎样的公司

    syniverse是一家怎样的公司?(详见问题描述)? 李超   核心业务当然是国际漫游了.简单来说就是做全球各个运营商之间的hub. 打个比方说,一家运营商A做通信,它的覆盖范围肯定是有限的(比如中 ...

  7. Mac佳软之Understand---Android源码分析阅读神器

    下载地址, 密码:gebp 供大家体验, 请大家支持正版!!! https://www.jianshu.com/p/06f25d9131de

  8. ie 使用window.open页面报错

    window.open(url)打开新页面是如果要通过地址栏来传参要注意 var qt = ""; qt += "&teachMaterialDealInfo.b ...

  9. 查询sqlserver中所有的数据库表 与 查询表中的说明注释字段

    1.查询数据库中所有的数据库表 SELECT * FROM sysobjects WHERE xtype = 'u' AND name != 'sysdiagrams'; 2.查询数据库表中的说明字段 ...

  10. 利用iis创建网站后为什么不能设置主机名

    主机名 主机名就是网站的域名,通俗说就是网站地址(如:www.baidu.com). 设置了主机名,而IIS确不知道主机名对应的地址在哪里. 举个例子,把www.baidu.com做为IIS网站的主机 ...