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. 'Invalid parameter not satisfying: body'

    afnetwork图片上传的时候出错,出现错误 2015-11-09 15:47:59.086 videoPro[3207:132795] *** Assertion failure in -[AFS ...

  2. 5,SFDC 管理员篇 - 数据模型 - 数据关联

    1,PickList 1,填写基本信息 2, 选择能角色的权限 3,在哪一个层上显示(object 上有多个 Record Type 对应多个层,需要选择在哪一个层显示) 4,Save   2,两个P ...

  3. Qlikview 处理交叉表数据

    数据来源于crossTable的时候,如何将数据做明细显示. 如图示交叉表数据 使用表格向导,选择交叉表按钮, 结果达到目的. 相关脚本. Month, 表示将要新加的字段的列明,Orders 为明细 ...

  4. spring security method security

    参考 Spring Security 官方文档 http://www.concretepage.com/spring/spring-security/preauthorize-postauthoriz ...

  5. C语言的time.h解释python的time

    clock_t clock(void),对比python的clock() 返回程序执行的时间,clock_t的实际类型是long #include<Windows.h> Sleep(); ...

  6. eclipse 删除所有注释及空白行

    Ctrl+F 删除java注释:  /\*{1,2}[\s\S]*?\*/ Ctrl+F 删除xml注释:  <!-[\s\S]*?--> Ctrl+F 删除空白行:   ^\s*\n 选 ...

  7. MySQL5.7.10免安装版配置

     最新版的 Mysql 不提供图形界面的安装了, 下载下来是一个压缩包的形式, 那么我们如何来使用它呢, 让它为我们工作呢? 环境: mysql-5.7.10-winx64 + win7(64位) 一 ...

  8. {Reship}{ListView}C# ListView用法详解

    ======================================================================== This aritcle came from http ...

  9. {Links}{Matting}{Saliency Detection}{Superpixel}Source links

    自然图像抠图/视频抠像技术发展情况梳理(image matting, alpha matting, video matting)--计算机视觉专题1 http://blog.csdn.net/ansh ...

  10. ecshop的弊病和需要修改的地方,持续更新

    ecshop的session机制是基于cookie的,用数据库进行缓存,当浏览器关闭cookie,sessions表会爆表,所以需要改进. 在cls_template.php中 $_echash值是固 ...