问题描述:

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. SpringBoot 整合携程Apollo配置管理中心

    携程官网对apollo的使用讲解了很多种方式的使用,但是感觉一些细节还是没讲全,特别是eureka配置中心地址的配置 这里对springboot整合apollo说一下 >SpringBoot启动 ...

  2. 1.2成员变量+类变量+static关键字

    成员变量和类变量的区别 由static修饰的变量称为静态变量,其实质上就是一个全局变量.如果某个内容是被所有对象所共享,那么该内容就应该用静态修饰:没有被静态修饰的内容,其实是属于对象的特殊描述. 不 ...

  3. Vue.extend提供自定义组件的构造器

    Vue.extend 返回的是一个“扩展实例构造器”,也就是预设了部分选项的Vue实例构造器.经常服务于Vue.component用来生成组件,可以简单理解为当在模板中遇到该组件名称作为标签的自定义元 ...

  4. Microsoft.EntityFrameworkCore.Sqlite的学习

    SQLite in ASP.NET Core with EntityFrameworkCore ASP.NET Core 2: Using SQLite as a light weight datab ...

  5. SAP月结操作讲解

    SAP月结操作讲解 https://wenku.baidu.com/view/ac6fe45d312b3169a451a4b9.html   步聚 操作内容 事务码 是否必须 操作时间 月/年结 1 ...

  6. ZOJ 3987 Numbers(Java枚举)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3987 题意:给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n ...

  7. 【Mysql】key 、primary key 、unique key 与index区别

    参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...

  8. HTML元素 绑定href属性

    给<li>标签绑定href属性 <ul class="honor-list clearfix"> <li style="cursor:poi ...

  9. Python统计list中各个元素出现的次数

    来自:天蝎圣诞结 利用Python字典统计 利用Python的collection包下Counter类统计 利用Python的pandas包下的value_counts类统计 字典统计 a = [1, ...

  10. Qt532界面.ZC测试

    ZC:Delphi中只要随便拖几个控件,设置一下属性就OK了.但是,Qt中 貌似没有 方便的方式来做这个... ZC:目前的解决方案是:Qt中 拖几个控件,然后点 工具条里面的 "栅格布局( ...