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. 转:IOS远程推送通知

    在ios系统中,app应用程序无法在后台完成较多的任务,仅仅允许程序做一些有限的任务(如音视频播放.地理位置信息.voip).然而,如果你想做 一些有趣的事情,并且告知用户,甚至用户没有使用你的app ...

  2. Gym 100418J Lucky tickets(数位dp)

    题意:给定一个n.求区间[1, n]之间的全部的a的个数.a满足: a能整除  把a表示自身二进制以后1的个数 思路:题意非常绕.... 数位dp,对于全部可能的1的个数我们都dfs一次 对于某一个可 ...

  3. ExtJs--16--Ext.override()方法专门用来重写对象的方法

    Ext.onReady(function(){ /** * Ext.override()方法专门用来重写对象的方法 */ //定义个类 Ext.define("U",{ //该类的 ...

  4. (转)c++ typedef 函数指针详细说明

    转自:http://blog.csdn.net/future200x/article/details/5350134 一个函数在编译时被分配一个入口地址,将这个入口地址称为函数的指针,可以用一个指针变 ...

  5. bzj1106: [POI2007]立方体大作战tet

    比较玄幻的题目. 考虑两个不同的元素 假设位置是 a...a...b...b... 那么不需要通过交换ab来消除ab,各自弄就行 若是 a...b...b...a... 那也没必要交换,先把b消掉就好 ...

  6. Java-MyBatis:MyBatis XML 文件

    ylbtech-Java-MyBatis:MyBatis XML 文件 1.返回顶部 1. Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大, ...

  7. Scrapy Architecture overview--官方文档

    原文地址:https://doc.scrapy.org/en/latest/topics/architecture.html This document describes the architect ...

  8. Axios 网络请求组件封装 (鉴权、刷新、拦截)

    一.前言 注意:本教程需要你对axios有一定的了解,不适用于小白(只能借鉴,希望你能自己动手),注释都写的很清楚.此封装并非完整版,已进行部分删减修改操作,但仍然适用于大部分业务场景,如果不适用于你 ...

  9. intell-

    intellect: n.[U, C] the ability to think in a logical way and understand things, especially at an ad ...

  10. ZBrush创建人体模型-ZBrush中ZSphere的基本使用

    本教程我们将学习ZSphere(Z球)在ZBrush®中的基本使用情况,了解它在个人创作过程中发挥着怎样的作用.作为ZBrush中的独特功能之一,ZSphere能够让用户通过清晰的拓扑结构创建基础模型 ...