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. T-MAX-冲刺总结

    T-MAX-冲刺总结 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 T-MAX 这个作业的目标 冲刺总结 作业的正文 T-MAX-冲刺总结 其他参考文献 面向B站,百度 ...

  2. 响应面分析 | response surface analysis | R代码

    先开题,慢慢补充. 参考: 什么是响应面(RSM)分析 Response-Surface Methods in R, Using rsm In-class Examples with R Code R ...

  3. windows powershell学习

    PowerShell,从名字可以知道,他首先是一个shell,shell的意思就是和Linux的bash等一样.和原来的cmd一样就是在里边敲命令(可执行文件)使用: 而Power就意味他是一个功能强 ...

  4. Apache 使用ssl模块配置HTTPS(Centos7 httpd2.4.6)

    根据原文:http://blog.csdn.net/ithomer/article/details/50433363改编 Web服务器在默认情况下使用HTTP,这是一个纯文本的协议.正如其名称所暗示的 ...

  5. Docs-.NET-C#-指南-语言参考-关键字-值类型:char

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型:char 1.返回顶部 1. char(C# 参考) 2019/10/22 char 类型关键字是 .NET System.C ...

  6. Flutter 中的常见的按钮组件 以及自定义按钮组件

    Flutter 里有很多的 Button 组件很多,常见的按钮组件有:RaisedButton.FlatButton. IconButton.OutlineButton.ButtonBar.Float ...

  7. vue模块化以及封装Storage组件实现保存搜索的历史记录

    <template> <div id="app"> <input type="text" v-model='todo' @keyd ...

  8. OpenBLAS编译 Debug x64 Win10 vs2015

    OpenBLAS编译  Debug x64  Win10  vs2015 >------ 已启动生成: 项目: ZERO_CHECK, 配置: Debug x64 ------ > Che ...

  9. IDEA中spring约束文件报红 的解决办法

  10. ABAP DEMO33 选择周的搜索帮助

    效果图 *&---------------------------------------------------------------------**& Report YCX_02 ...