118. Pascal's Triangle杨辉三角形(全部/一行)
[抄题]:
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
- 要返回数据结构的,先新建 确保类型了再cc
[奇葩corner case]:
[思维问题]:
不知道怎么表示
[一句话思路]:
做好每一行后再放进去
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 不熟悉双重嵌套:内部数组处理完了要加到外部数组中去
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
row 数组是每次临时新建的
[总结]:
- 要返回数据结构的,先新建 确保类型了再cc
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
arraylist.add即可
[关键模板化代码]:
add两次:
for (int j = 0; j < i + 1; j++) {
if (j == 0 || j == i) {
row.add(1);
}else {
row.add(triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j));
}
}
//add again
triangle.add(new ArrayList(row));
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
class Solution {
public List<List<Integer>> generate(int numRows) { //ini
List<List<Integer>> triangle = new ArrayList<List<Integer>>(); //cc
if (numRows <= 0) return triangle; //for
for (int i = 0; i < numRows; i++) {
List<Integer> row = new ArrayList<Integer>(); for (int j = 0; j < i + 1; j++) {
if (j == 0 || j == i) {
row.add(1);
}else {
row.add(triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j));
}
}
//add again
triangle.add(new ArrayList(row));
} //return
return triangle;
}
}
[代码风格] :
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
[抄题]:
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
只处理一行即可
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 用get存取改变数组值的时候要用temp暂存
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
额我也不知道j为什么要倒序相加,直接背吧
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
倒序+暂存
for (int i = 1; i <= rowIndex; i++) {
for (int j = i - 1; j >= 1; j--) {//remember reverse
int tmp = ret.get(j - 1) + ret.get(j);//
ret.set(j, tmp);
}
[其他解法]:
[Follow Up]:
做题先看这个,免得答案不一样,费事
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public List<Integer> getRow(int rowIndex) {
//ini
List<Integer> ret = new ArrayList<Integer>(); //cc
if (rowIndex <= 0) {
return ret;
} //for
ret.add(1);
for (int i = 1; i <= rowIndex; i++) {
for (int j = i - 1; j >= 1; j--) {//remember reverse
int tmp = ret.get(j - 1) + ret.get(j);//
ret.set(j, tmp);
}
ret.add(1);
} //return
return ret;
}
}
118. Pascal's Triangle杨辉三角形(全部/一行)的更多相关文章
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- 118. 119. Pascal's Triangle -- 杨辉三角形
118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- 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 ...
- 118. Pascal's Triangle (java)
问题描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
随机推荐
- (三十八)js之柯里化
先给大家介绍什么是柯里化与反柯里化 百度翻译: 在计算机科学中,柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的 ...
- Java进阶知识点4:不可变对象与并发 - 从String说起
一.String的不可变特性 熟悉Java的朋友都知道,Java中的String有一个很特别的特性,就是你会发现无论你调用String的什么方法,均无法修改this对象的状态.当确实需要修改Strin ...
- logistic 回归Matlab代码
function a alpha = 0.0001; [m,n] = size(q1x); max_iters = 500; X = [ones(size(q1x,1),1), q1x]; % app ...
- LeetCode Split Array into Consecutive Subsequences
原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...
- gatsbyjs 了解
1. 模型 2. 总结&&资料 从模型上可以看出和jamstack 提出的架构模型比较相似,可以看成是一个具体的实现,功能还是比较强大的 https://www.gatsbyjs.o ...
- Aix之 xmanager 2.0连接AIX服务器
xmanager连接AIX服务器可以分为两种情况:1.连接IBM服务器,使用远程桌面功能进行系统维护.要求这台服务器已经安装了图形桌面,如CDE等,并启动到图形界面.在xmanager中的Xbrows ...
- MySQL插入中文时出现ERROR 1406 (22001): Data too long for column 'name' at row 1 (转)
使用命令行方式登陆到MySQL服务器, 建立一个数据库,数据库编码设为UTF-8.此时,如果直接在命令行窗口使用insert语句插入中文,就遇到类似 ERROR 1406 (22001): Data ...
- java md5 函数
private static final String md5(final String s) { final String MD5 = "MD5"; try { // Creat ...
- DSP SYS/BIOS开发
https://blog.csdn.net/lg1259156776/article/details/80695318
- Velodyne 线性激光雷达数据合成
坐标系旋转 如果想用字母表示角度,有两个方法: 1. 用三角函数sind(θ4).cosd(θ4).tand(θ4).atand(θ4)进行表示,注意:θ4在输入时是角度,只是没有度数特有的符号(° ...