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. Java8中LocalDate的使用---项目中日期处理

    // 获取当前日期 LocalDate now = LocalDate.now(); // 设置日期 LocalDate now2 = LocalDate.of(2099, 2, 28); // 解析 ...

  2. [Java/File]读取日文CSV文件不乱码

    try { StringBuilder sb=new StringBuilder(); sb.append("\nContent in File:'"+filePathname+& ...

  3. you are not allowed to push code to protected branches on this project(转)

    .. 图 1-1 报错:failed to push some refs to 'http://*******.git'. 一痛瞎踅摸之后,远程控制电脑,在H电脑上,重新建立了一个test项目,之后走 ...

  4. vue.js的app.js太大怎么优化?

    vue.js的app.js太大怎么优化? # http://nginx.org/en/docs/http/ngx_http_gzip_module.htmlgzip on;gzip_min_lengt ...

  5. tomcat NIOEndpoint中的Acceptor实现

    EndPoint的组件就是属于连接器Connector里面的.它是一个通信的端点,就是负责对外实现TCP/IP协议.EndPoint是个接口,它的具体实现类就是AbstractEndpoint,而Ab ...

  6. Java基础 hello world基础实例

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  7. flutter Slider滑块组件

    滑块,允许用户通过滑动滑块来从一系列值中选择. import 'package:flutter/material.dart'; class SliderDemo extends StatefulWid ...

  8. ISO/IEC 9899:2011 条款6.5.9——相等操作符

    6.5.9 相等操作符 语法 1.equality-expression: relational-expression equality-expression    ==    relational- ...

  9. 使用log4j将日志输送到控制台、文件或数据库中

    转: 使用log4j将日志输送到控制台.文件或数据库中 2018-09-07 00:45:08 keep@ 阅读数 2880更多 分类专栏: 其它   版权声明:本文为博主原创文章,遵循CC 4.0 ...

  10. 【swoole】PHP+Swoole+Linux实现进程监控

    脚本代码 class Server { const PORT = 8888; public function port() { //netstat -anp 2>/dev/null| grep ...