给定 numRows, 生成帕斯卡三角形的前 numRows 行。
例如, 给定 numRows = 5,
返回
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
详见:https://leetcode.com/problems/pascals-triangle/description/

Java实现:

class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(numRows==0){
return res;
}
List<Integer> row = new ArrayList<>();
//ArrayList中的set(index, object)和add(index, object)的区别:set:将原来index位置上的object的替换掉;add:将原来index位置上的object向后移动
for (int i = 0; i < numRows; i ++) {
row.add(0, 1);
for (int j = 1; j < row.size() - 1; j ++) {
row.set(j, row.get(j) + row.get(j + 1));
}
res.add(new ArrayList<>(row));
}
return res;
}
}

118 Pascal's Triangle 帕斯卡三角形 杨辉三角形的更多相关文章

  1. 118. Pascal's Triangle

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

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

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

  3. LN : leetcode 118 Pascal's Triangle

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

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

  5. 118. Pascal's Triangle杨辉三角形(全部/一行)

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

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

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

  7. LeetCode 118 Pascal's Triangle

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

  8. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

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

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

随机推荐

  1. Consul环境搭建

    大家在玩的时候 一定要使用ningx 1.9以上版本啊! 下载:wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_ ...

  2. mac安装python3

    http://www.jianshu.com/p/51811fa24752 brew install python3 安装路径:/usr/local/Cellar 使用: 执行python3即可 配置 ...

  3. java反射技术实例

    java反射技术实例​1. [代码][Java]代码     package com.gufengxiachen.java.reflectiontest; public class Person {p ...

  4. Android应用资源---动画资源(Animation Resources)

    有两种类型的动画资源: 属性动画 在设定的时间内,通过修改与Animator类相关的对象的属性值来创建一个动画. 视图动画 有两种类型的视图动画框架 补间动画(Tween animation):通过执 ...

  5. 【应用】SVG饼状图

    <!DOCTYPE html> <html> <head> <title></title> </head> <body o ...

  6. C语言文件读写总结

    主要有四种: 1.文件的字符输入输出函数 fgetc fputc2.文件的字符串输入输出函数 fgets fputs3.文件的格式化输入输出函数 fscanf fprintf4.文件的数据块输入输出函 ...

  7. H264解码器源码(Android 1.6 版)

    H264解码器源码,移植ffmpeg中的H264解码部分到Android,深度删减优化,在模拟器(320x480)中验证通过. 程序的采用jni架构.界面部分,文件读取,视频显示都是用java做的,底 ...

  8. poj1151 Atlantis——扫描线+线段树

    题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...

  9. 2.12 Hivet中order by,sort by、distribute by和cluster by

    https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy 一.order by 对全局数据的排序,仅仅只有一个red ...

  10. 安装mosquitto报缺少dll文件的错误

    解决:下载缺少的dll组件,放到安装目录.  报错:The procedure entry point CRYPTO_memcmp could not be located in the dynami ...