Pascal's Triangle:

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
已知行数生成帕斯卡三角。实际上只要有第i层,那么就能生成第i+1层。每次新生成的层加入最终集合中即可。
     public List<List<Integer>> generate(int numRows) {
List<List<Integer>> re = new ArrayList<List<Integer>>(); for(int i=0;i<numRows;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(i-1).get(j-1)+re.get(i-1).get(j));
}
re.add(temp);
}
return re;
}
 

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即可。

     public List<Integer> getRow(int rowIndex) {
List<Integer> re = new ArrayList<Integer>();
for(int i=0;i<=rowIndex;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(j-1) + re.get(j));
}
re = temp;
}
return re;
}

[Leetcode][JAVA] Pascal's Triangle I, II的更多相关文章

  1. 【leetcode】Pascal's Triangle I & II (middle)

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

  2. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

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

  4. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  5. Java for 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 ...

  6. 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, ...

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

  8. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  9. 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, ...

随机推荐

  1. MAXIMO-IBM文件夹的笔记

    MAXIMO--IBM整套文件的目录结构及常用的文件说明: applications文件装的maximo的最重要的文件,包括ear包等发布文件. 进入applications文件夹里,maximo文件 ...

  2. struts2 xml中重定向

    <result name="success" type="redirect">ManagePartAction</result> 重定向 ...

  3. shell-自动更改LINUX服务器IP

    #!/bin/bash echo  echo   ==  fi i= newgateway= newhostname= cat >>$ipfile<<EOF IPADDR=&q ...

  4. 线程死锁情况和while在线程的作用

    public class printDemo04 { public static void main(String[] args) { Resource01 resource01 = new Reso ...

  5. 定位frame 中的对象

    在web 应用中经常会出现frame 嵌套的应用,假设页面上有A.B 两个frame,其中B 在A 内,那么定位B 中的内容则需要先到A,然后再到B.switch_to_frame 方法可以把当前定位 ...

  6. C# 命令绑定

    在构建WinForm程序中,为了使系统的结构清晰,有好的用户交互体验,实现不同按钮之间的交互,不使主窗体里面的代码臃肿.将按钮的命令通过类进行绑定,实现命令的管理使很有必要的. 该文章是将如何实现Bu ...

  7. 修改navigationbar右侧按钮离屏幕边缘位置

    先上代码 UIButton *settingBtn = [Utils creatCustomButtonWithFrame:CGRectMake(, , , ) btnTitle: titleColo ...

  8. 循环不变量loop invariant 与 算法的正确性

    在论述插入排序的正确性的时候, 书中引入了循环不变量的概念, 刚开始稍微有点不太明白, 早上查了一波资料之后决定把自己的理解记录下来. 什么是循环不变量 ? 在我看来, 所谓循环不变量的就是一个在循环 ...

  9. SQL Server 【CTE + FOR XML PATH】使用笔记~

    CREATE FUNCTION [dbo].[Getxxxxxxxxx] ( @productCategoryId INT, @SplitChar varchar ) RETURNS NVARCHAR ...

  10. mvc深入理解

    对于v和c好理解, 对于model分为数据对象模型和业务逻辑模型,一般为一个类,数据对象模型包含对一个具体数据表的相关操作,业务逻辑模型为处理一些业务逻辑.