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?

public class Solution {
public List<Integer> getRow(int rowIndex) {
if(rowIndex<0){
return null;
}
List<List<Integer>> list=new ArrayList<>();
if(rowIndex>=0){
List<Integer> l=new ArrayList<>();
l.add(1);
list.add(l);
}
if(rowIndex>=1){
List<Integer> l=new ArrayList<>();
l.add(1);
l.add(1);
list.add(l);
} if(rowIndex>=2){
for(int i=2;i<=rowIndex;i++){
List<Integer>l=new ArrayList<>();
List<Integer>prev=list.get(i-1);
l.add(1);
for(int j=1;j<=i-1;j++){
l.add(prev.get(j-1)+prev.get(j));
}
l.add(1);
list.add(l);
}
}
return list.get(rowIndex);
}
}

Others' Solution

 public List<Integer> getRow(int rowIndex) {
List<Integer> list = new ArrayList<Integer>();
if (rowIndex < 0)
return list; for (int i = 0; i < rowIndex + 1; i++) {
list.add(0, 1);
for (int j = 1; j < list.size() - 1; j++) {
list.set(j, list.get(j) + list.get(j + 1));
}
}
return list;
}

每天一道LeetCode--119.Pascal's Triangle II(杨辉三角)的更多相关文章

  1. [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 ...

  2. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

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

  3. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  4. [LeetCode] 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, ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  6. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

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

  8. leetcode 119 Pascal's Triangle II ----- java

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

  9. Java [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 ...

  10. C#解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  ...

随机推荐

  1. cocos2dx搭建开发环境

    windows7 64位 搭建cocos2dx 版本开发环境 目前cocos2dx分为2.x版本和3.x版本,搭建环境稍有不同 先搭建3.1版本win32开发环境 相关准备: 注意:安装路径尽可能不要 ...

  2. Windows-to-go-带着win10满街跑

    一.前言 有句话是这么说的,程序员对工作是时刻准备着的.无论你是长假还是短假,只要有网,你就躲不开客户.这样子,当你外出的时候你可以选择时刻背着电脑,因为你的电脑有着你顺手的开发工具以及开发环境.我们 ...

  3. C#中的where从句

    C#中的where从句 2011-07-03 13:07OrphousV | 分类:C#/.NET | 浏览8443次 能解释一下下面两段代码中where的作用吗?using System;publi ...

  4. Android下结束进程的方法

    转自:http://www.cnblogs.com/crazypebble/archive/2011/04/05/2006213.html 最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这 ...

  5. 进入名企必读的.NET面试题

    1. 罗列ASP.NET服务器控件的运行的生命周期. 一般服务器控件的生命周期包含11个阶段: /// <summary> /// 1. 初始化 /// </summary> ...

  6. Android访问WebService的两种方法

    首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.详细见:http://baik ...

  7. PowerShell管理IIS(新建站点、应用程序池、应用程序、虚拟目录等)

    #导入IIS管理模块 Import-Module WebAdministration #新建应用程序池 api.dd.com New-Item iis:\AppPools\api.dd.com Set ...

  8. 华为C语言笔试题集合

    ①华为笔试题搜集 1.static有什么用途?(请至少说明两种)     1)在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.     2) 在模块内(但在函数体外),一个被声明为 ...

  9. Lambda表达式实现有限状态机

    实现状态机有多种模式,其中最灵活而强大的方式是通过迁移表来实现,该方式的缺点之一是需要编写大量小块代码去支持迁移表.而在C#3.0中,可以以一种非常优雅的方式实现. 除了有限状态机外,还有有限自动机, ...

  10. [AngularJS] Lazy loading Angular modules with ocLazyLoad

    With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...