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 ...
随机推荐
- 如何快速上手.net下单元测试工具NUnit?
NUnit基本使用 准备知识: 读此博文需要了解单元测试基本概念及NUnit的的安装. 传送门:单元测试之道(使用NUnit) 1.常见的错误 当学习一个新东西时,先学习错误,是最快的方式. 1.1 ...
- C++中继承关系中的同名隐藏和对策
在C++及其面向对象的理论中,有这样的场景:一个类继承自另外一个类,如果这两个类都有一个函数名和参数及其返回值一样的成员函数,那么子类的函数会自动将父类对应的函数隐藏.即同名隐藏.在有时的开发过程中, ...
- 51nod 1012 最小公倍数LCM
输入2个正整数A,B,求A与B的最小公倍数. 收起 输入 2个数A,B,中间用空格隔开.(1<= A,B <= 10^9) 输出 输出A与B的最小公倍数. 输入样例 30 105 输出 ...
- iOS当前屏幕截屏
需求描述: 有两个ViewController 我们记做 A.B ,其中B controller只是显示下半部分: 如下图效果: 实现这种的方案很多,可以用添加View方法, 也可以用UIWindo ...
- c#模拟键盘输入
System.Windows.Forms.SendKeys.SendWait("j");
- 管理11gRAC基本命令 (转载) 很详细
在 Oracle Clusterware 11g 第 2 版 (11.2) 中,有许多子程序和命令已不再使用: crs_stat crs_register crs_unregiste ...
- Eclipse 进入前选择Workspace
如果选择了默认的Workspace会有一个问题. 打开一个workspace的时候,再次打开eclipse会报错,提示当前workspace正在被使用,然后让选择workspace. 最好的方法是每次 ...
- Oracle查询脚本优化
发现生产环境的Oracle数据库cpu使用率上升超过70%,其中一条查询语句达到每秒调用40多次.现在我们来观摩下该语句: select t.id,t.level,t.policy, t.type,t ...
- IE兼容模式与非兼容模式下jq的写法
1. $("#LabelRepeatType").removeAttr("disabled"); $("#LabelF ...
- php redis 命令合集
1.https://www.cnblogs.com/aipiaoborensheng/p/5666005.html 2.https://www.cnblogs.com/doanddo/p/734908 ...