【LEETCODE】33、LeetCode的Given a non-negative integer numRows, generate the first numRows of Pascal's triangle
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的更多相关文章
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【一天一道LeetCode】#118. Pascal's Triangle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- [leetcode]Pascal's Triangle @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
随机推荐
- Assignment5: 使用Visual Studio 进行可编码的UI测试
一.实验目的: 使用Visual Studio 可编码的UI测试功能创作自动化测试 二.实验原理:黑盒测试 三.实验需求:win8 app.Visual Studio Ultimate 2012/20 ...
- 磁盘性能指标--IOPS与吞吐量
磁盘性能指标--IOPS---------------------------------------------------------- IOPS (Input/Output Per ...
- vbs msgbox提示信息最前面显示
msgbox strContent, vbOKOnly or vbExclamation or vbSystemModal,strTitle 提示框类型列表: 常数 值 描述 vbOKOnly 0 只 ...
- 【Tomcat】Tomcat 原理架构(一)
Tomcat是什么 开源的 Java Web 应用服务器,实现了 Java EE(Java Platform Enterprise Edition)的部 分技术规范,比如 Java Servlet.J ...
- mac QQ 语音或视频时其他声音变小的解决办法
在使用MacBook的时候,使用QQ视频的同时 看视频 听歌都会发现,QQ视频声音正常,其他软件的声音会很小很小.怎么解决呢,首先 开启QQ后,在视频之前: 1.打开终端输入以下命令. printf ...
- 深入理解ElasticSearch(PDF版 内含目录)
深入理解ElasticSearch 介绍: 本书涵盖了Elasticsearch的许多中高级功能,并介绍了缓存.ApacheLucene库以及监控等模块的内部运作机制.其中,还涉及一些实用案例,比如配 ...
- 数据标记系列——图像分割 & Curve-GCN
在之前的文章中(参考:),我们提到了Polygon-RNN++在数据标注中的应用.今天不得不提到多伦多大学与英伟达联合公布的一项最新研究:Curve-GCN的应用结果显示图像标注速度提升10倍. Cu ...
- LM_ReadImgMode.js PC单页轮播读图模式组件,零依赖!
LM_ReadImgMode.js PC单页轮播读图模式组件,零依赖! github:http://dtdxrk.github.io/LM-ReadImgMode/ TXT 1.全新的2.0版本,脱离 ...
- 09点睛Spring MVC4.1-异步请求处理(包含兼容浏览器的服务器端推送)
转发地址:https://www.iteye.com/blog/wiselyman-2215852 9.1 异步请求处理 Servlet 3开始支持异步请求处理 Spring MVC 3.2开始支持S ...
- Django:将后台返回的数据填充到select下拉框中
select选择框如下: <select data-placeholder="选择项目..." class="form-control" name=&qu ...