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,3,1].

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


【思路】

一刷:我们为了满足空间复杂度的要求,我们新建两个ArrayList,一个负责存储上一个Pascal行的结果,一个根据上一个Pascal行得出当前Pascal行的结果,这个过程循环进行。

二刷:ArrayList的操作是比较慢的,这次我们用数组来对结果进行存储。数组保存上一个状态,通过上一个状态计算下一个状态。例如:[1,3,3,1],我们可以从最后一个元素开始,num[i] = num[i] + num[i-1]。最后我们再在末尾补1。这样能大大提高代码的效率。


【java代码一刷】

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

 


【java代码二刷】

 public class Solution {
public List<Integer> getRow(int rowIndex) {
int[] nums = new int[rowIndex+1];
for(int i = 0; i <= rowIndex; i++) {
for(int j = i; j >= 1; j--)
nums[j] += nums[j-1];
nums[i] = 1;
} List<Integer> res = new ArrayList<>(rowIndex);
for(int num : nums) res.add(num);
return res;
}
}
 

LeetCode OJ 119. Pascal's Triangle II的更多相关文章

  1. 【一天一道LeetCode】#119. Pascal's Triangle II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 【LeetCode】119. Pascal's Triangle II

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

  3. Leetcode No.119 Pascal's Triangle II(c++实现)

    1. 题目 1.1 英文题目 Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's tria ...

  4. 【LeetCode OJ】Pascal's Triangle II

    Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...

  5. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

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

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

  7. 119. Pascal's Triangle II@python

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  8. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  9. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

随机推荐

  1. C# 三角形外心和外接圆半径计算方法

    在网上找了好久,想找一个现成的方法来用,折腾半天发现没有一个好用的,最后迫不得已自己写了一个,需要的同学可以直接拿去用, private void GetTriangleExcenterRadius( ...

  2. 目标检测中proposal的意义

    在目标检测中,从很早就有候选区域的说法,也是在2008年可能就有人使用这个方法,在2014年的卷积神经网络解决目标检测问题的文章中,这个候选框方法大放异彩,先前的目标检测方法主要集中在使用滑动窗口的方 ...

  3. Java 后台sql注入

    JdbcTemplate.update(sql, ArrayList.toArray()) Connection conn = null; PreparedStatement ps = null; c ...

  4. CSU 1806 Toll

    最短路,自适应$Simpson$积分. 看了别人的题解才知道有个东西叫自适应$Simpson$积分. 有这样一个积分公式:$\int_a^b {f(x)dx}  \approx \frac{{b - ...

  5. 技能学习经验与C语言学习调查

    技能学习经验与C语言学习调查 前言 要说的话,这还是我第一次写博客.不论是为了作业也好,为了将来的学习工作也好,写博客都是必不可少的,也算是个自我提升的途径吧.不过第一次写博客,就用从来没听说过的ma ...

  6. table表头thead固定

    <html> <head> <meta charset="utf-8"/> <script type="text/javascr ...

  7. android相关内容

    一: 前台进程: 前台的进程的优先级最高, 可见进程: android系统一般存在少量的可见进程. 服务进程: 没有用户界面, 后台进程: 一般存在较多的后台进程. 空进程: 不包括任何活跃组件的进程 ...

  8. iosAPP打包上架xcode中Archive提交成功以后,不提示构建版本问题

    最近在项目更新时遇到Archive提交到开发者中心成功后,一直不提示构建版本信息,可能导致的原因是由于ios10以后对于APP中调用手机相册或摄像头麦克风时需要配置plist文件,配置如下内容或许会解 ...

  9. Android数据库--Sqlcipher的使用(一)

    1.下载官方支持包:https://s3.amazonaws.com/sqlcipher/3.2.0/sqlcipher-for-android-community-v3.2.0.zip Github ...

  10. HTML1高级

    HTML头部 一.链接在新窗口打开如果要定义整个网页的链接在新窗口打开,只要在/head里定义/base target="_blank"就可以了</p> 二.文档描述1 ...