问题描述:

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

问题分析:

第n行的数据是在第n-1行的基础上计算出来的。是一个迭代的过程

解决方法:

//生成杨辉三角的前n行
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>(); //定义list
if(numRows <= 0) return list;
List<Integer> row = new ArrayList<Integer>(); //第一行
row.add(1);
list.add(row);
for(int i = 2; i <= numRows; i++){ //生成后面的n-1行数据
List<Integer> l = new ArrayList<Integer>(); //存储第i行数据
l.add(1);
row = new ArrayList<Integer>(list.get(list.size() - 1)) ; //获得上一行数据
for(int j = 0; j < row.size() - 1; j++){
int e = row.get(j) + row.get(j + 1);
l.add(e);
} l.add(1); list.add(l);
}
return list;
}

118. Pascal's Triangle (java)的更多相关文章

  1. leetcode 118 Pascal's Triangle ----- java

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

  2. 118. Pascal's Triangle

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

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

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

  4. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  5. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  6. Java for LeetCode 118 Pascal's Triangle

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

  7. Java [Leetcode 118]Pascal's Triangle

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

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

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

  9. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

随机推荐

  1. LIS|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int f[N], a[N]; int n; // ...

  2. hihoCoder week16 RMQ-ST算法

    RMQ问题 用的st表吧,读入数据挺多的,输出数据也挺多的 我还用了 cout<<endl;  T了.. 真的是 做题不带脑子的 #include <bits/stdc++.h> ...

  3. Python实现机器学习算法:朴素贝叶斯算法

    ''' 数据集:Mnist 训练集数量:60000 测试集数量:10000 ''' import numpy as np import time def loadData(fileName): ''' ...

  4. Dockerize PostgreSQL

    Dockerize PostgreSQL Installing PostgreSQL on Docker Assuming there is no Docker image that suits yo ...

  5. HDU 4557 Tree(可持久化字典树 + LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=4757 题意: 给出一棵树,每个结点有一个权值,现在有多个询问,每次询问包含x,y,z三个数,求出在x到y的路径上 ...

  6. JavaScript中 call和apply

    call()方法和apply()方法的作用相同,他们的区别在于接收参数的方式不同. 对于call(),第一个参数是this值没有变化,变化的是其余参数都直接传递给函数.(在使用call()方法时,传递 ...

  7. git界面里面复制粘贴

    git界面里常用的粘贴方法: 1.Insert键或者Fn+Insert键 2.右键或者shift+右键 Paste 粘贴 3.还有一些设置可以用,在git面板中(或者右键),点击option选项 这里 ...

  8. java——File

    注意事项: 1:创建File对象需要导包, import java.io.File 2:File对象没有无参数构造.创建对象需要传参. 3:File类的对象,既可以代表文件也可以代表文件夹.   构造 ...

  9. typeScript入门基础 (1)

    1.ts是js的超集,可使用es5,es6的代码 2. ts的安装与编译: a.  首先需要Node.js环境 .  相信都有,略过. 不会的请百度,或者留言. b.  npm  install  - ...

  10. arcgis10.3 python2.7 开发环境配置

    如题: 首先安装arcgis desktop 和 自带的python版本,本楼的是2.7.8,安装地址在E:\PYTHON2.7,安装完成后即可开始安装pythonwin2.7,下载地址为:http: ...