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 ...
随机推荐
- iOS当前屏幕截屏
需求描述: 有两个ViewController 我们记做 A.B ,其中B controller只是显示下半部分: 如下图效果: 实现这种的方案很多,可以用添加View方法, 也可以用UIWindo ...
- BZOJ4560 [JLoi2016]字符串覆盖
题意 字符串A有N个子串B1,B2,-,Bn.如果将这n个子串分别放在恰好一个它在A中出现的位置上(子串之间可以重叠) 这样A中的若干字符就被这N个子串覆盖了.问A中能被覆盖字符个数的最小值和最大值. ...
- codevs4189字典
沙茶 题目大意:求某一个字符串前缀有没有在n个字符串前缀里出现过 题解:Trie树 查询前缀有没有出现 代码: //codevs4189 #include<iostream> #inclu ...
- HTML <meta> http-equiv Attribute 说明
1. 说明 Value Description content-type Specifies the character encoding for the document. Example: & ...
- JAMstack 最佳实践
摘自官方介绍,没有翻译(没必要,已经比较简单了,重要的就是进行每条的诠释了,后续...) Entire Project on a CDN Because JAMstack projects don’t ...
- C++输入流和输出流、缓冲区
一.C++输入流和输出流 输入和输出的概念是相对程序而言的. 键盘输入数据到程序叫标准输入,程序数据输出到显示器叫标准输出,标准输入和标准输出统称为标准I/O,文件的输入和输出叫文件I/O. cout ...
- 【Python学习笔记】输出现在的时间
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 2016-01-27 21:40:25
- 上一步是硬件描述语言,下一步是FPGA
上一步是硬件描述语言,下一步是FPGA. 学习了硬件描述语言(Verilog或者VHDL)之后,FPGA该如何继续. 世上没有捷径,每一步都得踏踏实实的走.学习FPGA也是这样,在有了硬件描述语言的基 ...
- git在windows上的安装和简单使用
git在windows上的安装和简单使用. 参考: https://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E8%BF%9C%E7%A8%8B%E ...
- EasyUI treegrid 加载checked
EasyUI treegrid 加载checked $(function () { $('#tbDictContTree').treegrid({ title: '数据字典目录管理', iconCl ...