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中存放的就是答案。

代码如下:

 public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> answer = new ArrayList<Integer>();
answer.add(1); for(int i = 1;i <= rowIndex;i++){
List<Integer> temp = new ArrayList<Integer>();
temp.add(1);
for(int j = 1;j <= i-1;j++){
temp.add(answer.get(j-1)+answer.get(j));
}
temp.add(1);
answer = temp;
} return answer;
}
}

【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. Unity中surfaceShader的处理机制和finalColor

    http://blog.csdn.net/swj524152416/article/details/52945375

  2. 纯JS实现动态时间

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. java jdbc连接数据库,Properties 属性设置参数方法

    今天在整合为数据库发现在配置中实现的赋值方式,可以用代码实现.特记录下共以后参考: 代码:        // 操作数据库        Connection conn; String strData ...

  4. tomcat下发布项目,遇到的问题总结

    以前一直是在eclipse下启动tomcat,然后访问web项目.今天脑门一热,就想用tomcat的bin目录下的startup.bat来启动tomcat,虽然tomcat的启动很顺利,但是访问网页的 ...

  5. List和Set排序的实现

    List.Set.Map的区别 List和Set继承了Collection接口. List以特定索引来存取元素,可以有重复元素.Set不能存放重复元素(用对象的equals()方法来区分元素是否重复) ...

  6. POJ 2187 Beauty Contest【凸包周长】

    题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  7. Xshell调节字体大小和样式

    有时候没有看着字体太小的,好难受, 调节字体大小: ALT+P快捷键打开

  8. centos出现-bash: /usr/bin/php: 没有那个文件或目录解决方法

    造成这个的原因是因为找不到php的执行文件导致的,原先我是安装的php5.4,然后卸载了重新安装php7,导致php可执行文件没有放到$PATH中,可以在终端测试:php -v,如果报错bash: / ...

  9. SAP号码段

    [转http://blog.csdn.net/wengyupeng/article/details/8513527] 1.通用Tcode:SNRO 常用维护特定Number range Tcode: ...

  10. IDEA main方法自动补全(转发:http://blog.csdn.net/zjx86320/article/details/52684601)

    最近刚从Eclipse转到IDEA,各种学习丫,IDEA里的main方法是不能自动补齐的,肿么办呢? 1.首先,点击File-->Settings-->Editor-->Live T ...