【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 ...
随机推荐
- Unity3D ACT动作游戏《武士2》项目源码附教程
武士二源码亲测unity2018.3.6能运行 仅供学习附有教程 教程完整73课,网上大部分一般为65课, 教程大小27GB,mp4格式 整理不易 扫码时备注或说明中留下邮箱 付款后如未回复请至htt ...
- [Java.File]如果写 File filesFolder = new File("/") ,在windows系统中,filesFolder 会去找哪个盘符? 答案:程序运行路径的根盘符.
首先这段代码在Unix/Linux系统上会去找根路径,但在Windows系统上会去找C:盘还是D:盘还是其它盘呢? 其实它会去找user.dir所在盘符的根目录,user.dir即用户的当前工作目录, ...
- Python3基础 str __add__ 拼接,原字符串不变
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- stl中map的四种插入方法总结
stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...
- Web Service 和 WCF的比较
Web Service 和WCF的比较 Web Service 的工作原理 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intra ...
- 【Java】阿里巴巴Java开发手册
阿里巴巴Java开发手册 下载地址:https://github.com/alibaba/p3c 阿里巴巴代码规范检查插件p3c 下载地址:https://github.com/alibaba/p3c
- Qt编写气体安全管理系统2-界面框架
一.前言 整体框架包括两个部分,一部分是UI界面框架,比如一级二级导航菜单按钮整体布局等,一部分是项目框架,上一篇文章说的是项目框架,这一篇文章来说界面框架,Qt做界面非常快速和高效,尤其是提供了可视 ...
- 转 How to Find Out Who Is Locking a Table in MySQL
MySQL is adding more tools to monitor its internals with every new release, but one thing it still l ...
- Form表单的传递与接收
目录 表单的构建 后端接收 创建model 用Model接收表单的后端 表单的构建 我才知道这个东西,在开发中经常遇到表单的情况.一下子提交一串内容.表单元素 form,里面的内容必须有name字段. ...
- 【k8s node断电重启】
kubernetes断电重启 导致部分pod无法删除 dashboard上处于黄色 kubectl get处于terminate 状态 kubectl delete报错: An error occur ...