给定 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. codeforces B. Roma and Changing Signs 解题报告

    题目链接:http://codeforces.com/problemset/problem/262/B 题目意思:给出 n 个数和恰好一共要做的操作总数k.通过对n个数进行k次操作,每次操作可以把a[ ...

  2. 对服务器上所有Word文件做全文检索的解决方案-Java

    一.背景介绍    Word文档与日常办公密不可分,在实际应用中,当某一文档服务器中有很多Word文档,假如有成千上万个文档时,用户查找打开包含某些指定关键字的文档就变得很困难,目前这一问题没有好的解 ...

  3. C语言中文件操作

    用两个指针变量来操作字符串. 多维数组在做函数参数的时候,会退化成为一个指针变量,变成一个指向一维数组的数组指针,注意,是一个指针变量. 一维数组在当作函数参数传递的时候,会退化成为一个对应类型的指针 ...

  4. 51nod-1065:最小正子段和(STL)

    N个整数组成的序列a11,a22,a33,…,ann,从中选出一个子序列(aii,ai+1i+1,…ajj),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的. 例如:4,-1 ...

  5. 使用PowerDesigner设计数据库

    1.快捷键CTRL+N 创建 New Model 选择如下图,并设置 Model name 单击OK 2.使用工具添加实体 双击Entity_1,填上如下图信息   切换选项卡,添加属性信息 其中 M ...

  6. SQL Server中查询CPU占用高的SQL语句

    SQL Server中查询CPU占用高的情况,会用到sys.sysprocesses ,dm_exec_sessions ,dm_exec_requests 一.查看当前的数据库用户连接有多少 USE ...

  7. Redis配置主从架构,实现读写分离

    Redis的主从架构,能帮助我们实现读多,写少的情况,下面配置Redis架构,很简单. 准备环境 vmware + rhel-server-7.0(101,102,103)+redis-3.2.0 1 ...

  8. 51nod1228

    伯努利数 这个是答案 其中的b是伯努利数,可以n^2预处理 伯努利数n^2递推 #include<bits/stdc++.h> using namespace std; typedef l ...

  9. Unity4.0配置

    关于Unity4.0的使用: 一 安装Unity 在程序包管理器控制台输入命令:Istall-Pckage unity.mvc安装后会在App_Start中生成UnityConfig.cs 和Unit ...

  10. msq 表操作与其数据类型

    一:表介绍 表相当于文件, 表中的一条记录就相当于文件的一行内容, 不同的是,表中的一条记录有对应的标题,称为表的字段: id,name, age, sex,称为字段, 其余的一行内容称为一条记录. ...