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. STM32实验非正式报告之DMA

    前言 DMA即直接内存存取.我理解它就是一个“交通部长”抑或是一个“搬运工”,协助CPU存储或读取数据.既然它的主要工作就是“搬运”数据,服务对象自然就是内存(不太严格的说法吧,STM32中Flash ...

  2. XML操作:1.XML类(http://blog.csdn.net/happy09li/article/details/7460521)

    XML绑定TreeView private void XmlOperation_Load(object sender, EventArgs e) { path = AppDomain.CurrentD ...

  3. 微软IIS服务器的最佳优化工具- IIS Tuner

      dudu的 <让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求>,里面涉及到需要手工调整参数的地方.在这篇文章中,我们给你介绍一个IIS ...

  4. Get-ChildItem参数之 -Exclude,Filter,Recurse应用

    $p = "D:\PSScript" gci $p -Exclude "UpdateLog" #排除子目录"UpdateLog",但是后面不 ...

  5. 配置集群Nginx+Memcached+Tomcat集群配置

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下配置集群 1.   Nginx Nginx是通过将多个Web Server绑定到同一个IP地址下,以实现多个WebS ...

  6. delphi 13 打印相关

    打印 页面设置 打印预览 文档属性     //---------------------------------------------------------------------------- ...

  7. SuperToolTips

    https://github.com/nhaarman/supertooltips supertooltips-master.zip

  8. IOS 常用第三方库

    名称 特性 效果图 FXBlurView 实时背景模糊效果   FDFullscreenPopGesture 让UINavigationController在屏幕任何位置均可滑动返回 NJKWebVi ...

  9. 日志分析(二) logstash patterns

    grok-patterns内置了很多基础变量的正则表达式的log解析规则,其中包括apache的log解析(同样可以用于nginx的log解析).   基于nginx日志分析配置: 1.配置nginx ...

  10. Word2010编号列表&多级列表

    1.引用场景         对于一份标准.漂亮的word文档,编号列表和多级列表的设置时必不可少的,正因为有它们,文档看起来才更专业,使用起来才更加的方便.如下面截图一般,这是十分常见的多级列表设置 ...