Pascal's Triangle II Leetcode java
题目:
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?
题解:
为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果。相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了。
下面讲解转自Alf的:
“如果没有这个O(k)空间的限制,那么可以一行一行迭代生成。如果要直接生成第i行,假设生成k=3,可以这样考虑这样的一个过程:
1 0 0 0 k = 0
1 1 0 0 k = 1
1 1 1 0
1 2 1 0 k = 2
1 2 1 1
1 2 3 1
1 3 3 1 k = 3
上述过程实际上就是一个in-place的迭代过程。每当生成下一行的时候,首先数组相应位置1,然后从右向左计算每一个系数。
”
代码如下:
- 1 public ArrayList<Integer> getRow(int rowIndex) {
- 2 ArrayList<Integer> result = new ArrayList<Integer>(rowIndex + 1);
- 3 for (int i = 0; i <= rowIndex; i++) {
- 4 result.add(0);
- 5 }
- 6 result.set(0, 1);
- 7 for (int i = 1; i <= rowIndex; i++) {
- 8 result.set(i, 1);
- 9 for (int j = i - 1; j > 0; j--) {
- result.set(j, result.get(j) + result.get(j - 1));
- }
- }
- return result;
- }
Reference:http://blog.csdn.net/abcbc/article/details/8982651
Pascal's Triangle II Leetcode java的更多相关文章
- Pascal's Triangle II —LeetCode
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Pascal's Triangle II leetcode
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Pascal's Triangle 2(leetcode java)
问题描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
- LeetCode算法题-Pascal's Triangle II(Java实现)
这是悦乐书的第171次更新,第173篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第30题(顺位题号是119).给定非负索引k,其中k≤33,返回Pascal三角形的第k ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- LeetCode OJ 119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- 【LEETCODE】34、119题,Pascal's Triangle II
package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...
- 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- ...
随机推荐
- strlen()和mb_strlen()
换行需要用双引号,单引号只会输出字符. strlen()返回字符串所占的字节数,对于utf8编码的中文,一个汉字占三个字节. mb_strlen()返回字符个数,如果不写第二个参数,就会使用内部编码, ...
- HTTP错误405
405 - 用来访问本页面的(方法不被允许) HTTP 错误 405 -禁止访问资源 HTTP 错误 405 405 不允许此方法 对于请求所标识的资源,不允许使用请求行中所指定的方法.请确保为所请求 ...
- 关于Android4.X的Alertdialog对话框
最近在做Android4.0的开发,发现AlertDialog相比较以前有了较大变化,就是在触摸对话框边缘外部,对话框消失 于是研究其父类发现,可以设置这么一条属性,当然必须先AlertDialog. ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- HDU 5836 Rubik's Cube BFS
Rubik's Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5836 Description As we all know, Zhu is ...
- shell 常用命令语句
查找并删除 sudo fing / -name '*fcitx*' | xargs sudo rm -rf find . -type d -name ‘.svn’ | xargs rm -rf fin ...
- SGU 104. Little shop of flowers (DP)
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...
- Programming 2D Games 读书笔记(第二章)
本意还是想了解DirectX的,由于网上拿不到书的pdf文档,幸好有作者的源代码示例,想完整的看一下,基本的游戏需要的点. 下面直接以代码为例,仅用于帮助自身理解 http://www.progr ...
- CAD扩展属性的提取--FME方式
一.CAD的扩展属性 了解一下CAD的扩展属性方式,CAD的扩展属性包括二类: 基于CAD二次开发的软件产品(例如南方cass),其扩展属性是附属在图形(点.多段线.注记.面)上面的,它是以XReco ...
- 某个 页面覆盖了 UITabBar 的tabItem的解决办法
将这个页面的背景色设置为无色: [self.view setBackgroundColor:[UIColor clearColor]]; 或者 self.view.frame = CGRectMake ...