package y2019.Algorithm.array;

import java.util.ArrayList;
import java.util.List; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: Generate
* @Author: xiaof
* @Description: Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
* Input: 5
* Output:
* [
* [1],
* [1,1],
* [1,2,1],
* [1,3,3,1],
* [1,4,6,4,1]
* ]
*
* 如果不是第一个数据和最后一个数据,那么中间的数据求值方式
* a[i,j] = a[i - 1][j-1] + a[i - 1][j]
*
* @Date: 2019/7/1 17:31
* @Version: 1.0
*/
public class Generate { //这个效率0ms,已经差不多最快了
public List<List<Integer>> solution(int numRows) { List<List<Integer>> result = new ArrayList<List<Integer>>(); //如果一行都没有,直接反馈空
if(numRows <= 0) {
return result;
} //遍历生成每一行数据
for(int i = 0; i < numRows; ++i) {
//a[i,j] = a[i - 1][j-1] + a[i - 1][j]
List row = new ArrayList();
for(int j = 0; j < i + 1; ++j) {
//求出每一行的数据
if(j == 0 || j == i) {
row.add(1);
} else {
row.add(result.get(i - 1).get(j - 1) + result.get(i - 1).get(j));
}
}
result.add(row);
} return result;
} //大牛答案,内存占用最低,差别再那?
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
if(numRows==0){
return res;
}
//这里吧row定义再外面
List<Integer> temp = new ArrayList<>();
temp.add(1);
res.add(temp);
for (int i = 2; i <= numRows; i++) {
try {
temp=res.get(i-2);
}catch (Exception e){
System.out.println("i = " + i);
}
//但是这里还是定义了row数据
List<Integer>x=new ArrayList<>();
//也许关键就是这个位置了,再外面+1,少一次循环
x.add(1); for(int j=0;j<temp.size()-1;j++){
x.add(temp.get(j)+temp.get(j+1));
}
x.add(1);
res.add(x);
}
return res;
} }

【LEETCODE】33、LeetCode的Given a non-negative integer numRows, generate the first numRows of Pascal's triangle的更多相关文章

  1. [LeetCode] Pascal's Triangle 杨辉三角

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

  2. LeetCode 118 Pascal's Triangle

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

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

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

  4. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  5. LeetCode——Pascal's Triangle

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

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

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

  7. 【一天一道LeetCode】#118. Pascal's Triangle

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

  8. [leetcode]Pascal's Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...

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

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

随机推荐

  1. vue 错误提问示例

    > 表格显示数据,选中其中一条数据进行编辑,弹出层编辑,发现修改数据时,表格中的数据同步变化.编辑成功之后,还会返回原本没修改之前的数据. > 传对象给子组件,子组件的值污染父组件.

  2. MySQL事务表和非事务表

    查看 max_binlog_stmt_cache_size 参数解释时,有这么一句话 If nontransactional statements within a transaction requi ...

  3. 第2课第2节_Java面向对象编程_封装性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之封装性:把属性和方法封装在一个整体,同时添加权限访问. 1.封装性的简单程序如下,看一下第19行,如果我们不对age变量进行权限的管控 ...

  4. 效率包括了代码的GC 大小与内存大小,执行速度等等。其中执行速度不是关注 的重点

    效率包括了代码的GC 大小与内存大小,执行速度等等.其中执行速度不是关注的重点

  5. Netty服务器连接池管理设计思路

    应用场景: 在RPC框架中,使用Netty作为高性能的网络通信框架时,每一次服务调用,都需要与Netty服务端建立连接的话,很容易导致Netty服务器资源耗尽.所以,想到连接池技术,将与同一个Nett ...

  6. 【转】Django继承AbstractUser新建User Model时出现auth.User.groups: (fields.E304)错误

    错误详情如下: (venv) D:\workspace\music>python manage.py makemigrations SystemCheckError: System check ...

  7. Python3基础 函数 多值参数 元组与字典形式(键值对分别指出)

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  8. JS 数字相加出现多个小数的问题

    今天在页面上用到了js进行小数相加119.01+0.01,结果大家都知道应该是:119.02的,然而结果是119..0200000…. ,莫名其妙的,还以为是我写的程序有问题,后来查了下才知道这是ja ...

  9. 安卓终端-Termux

    Termux是一个 Android 终端模拟器以及提供 Linux 环境的应用程序.跟许多其他应用程序不同,无需 root 设备也无需进行设置.它是开箱即用的!它会自动安装好一个最基本的 Linux ...

  10. 全面系统Python3入门+进阶-1-2 Python的特性

    结束