Description:

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]
] Thoughts:
第一种:直观的想法就是,我们计算第n+1行的值,它的头尾都是1,中间的值T[n+1][i] = T[n][i-1]+T[i];根据这个想法得到如下的java代码:
class Solution {
public List<List<Integer>> generate(int numRnows){
List<List<Integer>> result = new ArrayList();
if(numRnows<=0){
return new ArrayList(){{}};
}
for(int i = 1;i<=numRnows;i++){
List lists = new ArrayList(); if(i == 1){
lists.add(1);
}else{
lists.add(1);
for(int j = 1; j<i-1;j++){
lists.add(result.get(i-2).get(j-1)+result.get(i-2).get(j));
}
lists.add(1);
} result.add(lists);
}
return result;
} }

第二种想法:第i行有i个数;每次往最前面插入一个1,那么除了头尾以外,中间的值T[n][i] = T[n][i]+T[n][i+1], 根据这个想法得到如下的代码:

package easy.array;

import java.util.ArrayList;
import java.util.List; public class GeneratePascal {
public List<List<Integer>> generate(int numRnows){
List<List<Integer>> allrows = new ArrayList<List<Integer>>();
ArrayList<Integer> row = new ArrayList<Integer>();
for(int i=0;i<numRnows;i++)
{
row.add(0, 1);
for(int j=1;j<row.size()-1;j++)
row.set(j, row.get(j)+row.get(j+1));
allrows.add(new ArrayList<Integer>(row));
}
return allrows;
} public static void main(String[] args){
int numRnows = 10;
GeneratePascal ge = new GeneratePascal();
List<List<Integer>> f = ge.generate(numRnows);
for(int i= 0; i<numRnows;i++){
System.out.println(f.get(i));
}
}
}

Pascal Triangle的更多相关文章

  1. leetcode—pascal triangle

    1.题目描述 Given numRows, generate the first numRows of Pascal's triangle.   For example, given numRows ...

  2. 杨辉三角(Pascal Triangle)的几种C语言实现及其复杂度分析

    说明 本文给出杨辉三角的几种C语言实现,并简要分析典型方法的复杂度. 本文假定读者具备二项式定理.排列组合.求和等方面的数学知识. 一  基本概念 杨辉三角,又称贾宪三角.帕斯卡三角,是二项式系数在三 ...

  3. HDU 4237 The Rascal Triangle

    The Rascal Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. Pescal Triangle Two

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

  5. leetcode-pascal triangle I&&II

    对于第2个pascal triangle,通过观察可以发现,其实只需要2个额外的变量来记录,于是就设了个tmp数组. 整体有点DP问题中的滚动数组的感觉. #include <vector> ...

  6. Must practice programming questions in all languages

    To master any programming languages, you need to definitely solve/practice the below-listed problems ...

  7. 算法之杨辉三角形(Java语言)

    杨辉三角形, 又称贾宪三角形.帕斯卡三角形. 前9层写出来如下: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 ...

  8. leetcode 0217

    目录 ✅ 682. 棒球比赛 描述 解答 cpp py ✅ 999. 车的可用捕获量 描述 解答 c other java todo py ✅ 118. 杨辉三角 描述 解答 cpp py ✅ 258 ...

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

随机推荐

  1. Android自制浏览器WebView-android学习之旅(64)

    简单讲解如何使用WebView加载百度的网页 acticity代码 public class MainActivity extends Activity { private WebView webVi ...

  2. JAVA内部类_1

    使用内部类的原因: (1)可以访问该类定义所在作用域中的数据,包括私有数据. (2)可以对同一个包中的其它类隐藏起来. (3)当想要定义一个回调函数且不想编写大量代码时,使用匿名内部类比较便捷. 下面 ...

  3. 【一天一道LeetCode】#76. Minimum Window Substring

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

  4. ConcurrentHashMap和HashTable的区别

    hashtable是做了同步的,hashmap未考虑同步.所以hashmap在单线程情况下效率较高.hashtable在的多线程情况下,同步操作能保证程序执行的正确性. 但是hashtable每次同步 ...

  5. 【翻译】Ext JS——高效的编码风格指南

    原文:ExtJS - Efficient coding style guide 作者:Raja 切勿使用"new"关键字:在Ext JS中,使用"new"关键字 ...

  6. mpi中程序在集群中的分发

    我们在开发mpi程序时,由于其是分布式程序,我们在单个节点上完成编码后,需要将代码拷贝到整个集群进行测试.集群之间的文件拷贝可以通过scp命令完成.但是scp命令是针对两个节点之间文件互传设计,为了将 ...

  7. R--线性回归诊断(二)

    线性回归诊断--R [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt   勿忘初心  无畏未来 作为一个初学者,水平有限,欢迎交流指正. R--线性回 ...

  8. WebService开发指南

    WebServiceInAurora Web Service Web Service是一种面向服务的架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作.在Aurora框架 ...

  9. java的参数传递与内存分配问题

    本文可作为北京尚学堂java课程的学习笔记. 看下面这段代码. class BirthDate { private int day; private int month; private int ye ...

  10. ROS探索总结(十五)——amcl(导航与定位)

    在理解了move_base的基础上,我们开始机器人的定位与导航.gmaping包是用来生成地图的,需要使用实际的机器人获取激光或者深度数据,所以我们先在已有的地图上进行导航与定位的仿真. amcl是移 ...