Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,

Return [1,3,3,1].

Note:

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

原题链接:https://oj.leetcode.com/problems/pascals-triangle-ii/

题目:给定一个索引k,返回帕斯卡三角形的第k行。

思路 : 此题能够用上一题中的方法来解。直接就是通项公式,与k行之前的行没有关系。

也能够一次分配结果所需大小的list。每次计算一行,并将结果置于合适的位置,下一行採用上一行的结果进行计算。

	public static List<Integer> getRow(int rowIndex) {
if (rowIndex < 0)
return null; List<Integer> result = new ArrayList<Integer>(rowIndex + 1);
result.add(1); for (int i = 1; i <= rowIndex; i++) {
int temp1 = 1;
for (int j = 1; j < i; j++) {
int temp2 = result.get(j);
result.set(j, temp1 + temp2);
temp1 = temp2;
}
result.add(1);
} return result;
}

reference : http://www.darrensunny.me/leetcode-pascals-triangle-ii/

LeetCode——Pascal&#39;s Triangle II的更多相关文章

  1. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  2. 【Leetcode】Pascal&#39;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. LeetCode——Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  4. Pascal&#39;s Triangle II

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

  5. leetcode - Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  6. Pascal&#39;s Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...

  7. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  8. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  9. leetcode:Pascal&#39;s Triangle

    一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...

随机推荐

  1. Deming管理系列(1)——开车仅仅看后视镜

    问题: 当业务经理被要求为未来的业务做计划时,他会提出一个自觉得不错的数字,而董事会往往希望能获得更大的收益,多次与其谈判.而业务经理在这方面不是新手,他有非常多可用的报告. 为什么不能让业务规划流程 ...

  2. Spyder调试快捷键

    Ctrl+1:  注释.取消注释 Ctrl+4/5:  块注释 / 取消块注释 F12: 断点 / 取消断点 F5: 运行 Ctrl+F5: 启动调试 Ctrl+F10: 单步调试,跳过函数内部实现 ...

  3. 院校-美国:美国国立卫生研究院(NIH)

    ylbtech-院校-美国:美国国立卫生研究院(NIH) 美国国立卫生研究院(简称NIH)位于美国马里兰州贝塞斯达(Bethesda),是美国最高水平的医学与行为学研究机构,初创于1887年,任务是探 ...

  4. 使用VMware搭建3台一模一样的Linux虚拟机

    转自:https://www.linuxidc.com/Linux/2014-08/105909.htm 简介:VMware可以在个人本地一台笔记本机器上同时运行二个或更多Windows.DOS.LI ...

  5. Laravel-事件简单使用

    Laravel-事件简单使用 标签(空格分隔): php, laravel 注册事件和监听器 生成事件和监听器:php artisan event:generate key => 事件 valu ...

  6. POJ 1384 Piggy-Bank DP

    一个完全背包 很裸,对于我这种DP渣渣都能1A.. // by SiriusRen #include <cstdio> #include <cstring> #include ...

  7. Android 让系统自动生成缩略图并写入媒体库

    MediaStore.Video.Thumbnails.getThumbnail(ContentResolver cr, long origId, int kind, BitmapFactory.Op ...

  8. here.less

    <html><head><title>Test Less</title><link rel="stylesheet/less" ...

  9. 【AnjularJS系列4 】 — 单个页面加载多个ng-App

    第四篇,插播, 单个页面加载多个ng-App 在写范例的时候发现的问题 一个页面有多个ng-app,angular只会处理第一个ng-app 需要加载两个ng-app,需要进行手动加载: angula ...

  10. Java自定义属性注解

    代码: import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.ElementT ...