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?


题解:简单的模拟题,每次用answer列表存放上一层的值,用temp列表存放当前层的值,只要计算好temp中需要重新计算的元素的索引范围[1,i-1](第i层),然后根据answer计算就可以了,每次计算完一层更新answer为这一层的数,最后answer中存放的就是答案。

代码如下:

  1. public class Solution {
  2. public List<Integer> getRow(int rowIndex) {
  3. List<Integer> answer = new ArrayList<Integer>();
  4. answer.add(1);
  5.  
  6. for(int i = 1;i <= rowIndex;i++){
  7. List<Integer> temp = new ArrayList<Integer>();
  8. temp.add(1);
  9. for(int j = 1;j <= i-1;j++){
  10. temp.add(answer.get(j-1)+answer.get(j));
  11. }
  12. temp.add(1);
  13. answer = temp;
  14. }
  15.  
  16. return answer;
  17. }
  18. }

【leetcode刷题笔记】Pascal's Triangle II的更多相关文章

  1. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  2. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. 【leetcode刷题笔记】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  4. 【leetcode刷题笔记】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【leetcode刷题笔记】Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. 【leetcode刷题笔记】Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. 【LEETCODE】34、119题,Pascal's Triangle II

    package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...

  9. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

随机推荐

  1. 微信小程序之云开发一

    最近听说微信小程序发布了云开发,可以不需要购买服务器,就能开发小程序和发布小程序,对于动辄千元的服务器,极大的节约了开发成本,受不住诱惑,我就开始了小程序的云开发,目前项目已上线,亲测不收费,闲不住的 ...

  2. php扩展安装phpize

    安装php(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize 一.phpize是干嘛的? phpize是什么东西呢?php官方的说 ...

  3. P1009 阶乘之和

    P1009 阶乘之和 题目提供者洛谷OnlineJudge 标签数论(数学相关)高精1998NOIp提高组NOIp普及组 难度普及- 通过/提交1139/3791 提交该题 讨论 题解 记录 题目描述 ...

  4. MATLAB循环结构:while语句P69范数待编

    while语句的一般格式为: while 条件 循环体语句 end 从键盘输入若干个数,当输入0时结束输入,求这些数的平均值和它们的和. 程序如下: sum=; n=; x=input('输入一个数字 ...

  5. 九度OJ 1187:最小年龄的3个职工 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2291 解决:936 题目描述: 职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来. 输入: 输入第一行包括1个 ...

  6. php读写csv、xml文件: SimpleExcel

    实例结构: 1. csv2xml.demo.php <?php use SimpleExcel\SimpleExcel; // 这句不能少! require_once ('../lib/Simp ...

  7. PAT 组合数的和(15)

    给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入 ...

  8. ALV 表头 ADD_TEXT

    [转自http://lz357502668.blog.163.com/blog/static/16496743201252891452493/] CALL METHOD valid_reference ...

  9. ABAP下载服务器文件到本机

    转自http://blog.sina.com.cn/s/blog_701594f40100l8ml.html ABAP:下载服务器文件到本机 对服务器的文件进行读写操作,SAP提供了OPEN DATA ...

  10. 项目中nodejs包高效升级插件npm-check-updates

    nodejs包高效升级插件npm-check-updates 最近想升级npm的包 1.//常规的包升级方式/2.npm update (包) 到npm一搜发现了一个很好的升级工具 npm-check ...