LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II
Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution
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),与上一题 杨辉三角 类似,但是我们稍微改一下,只需要存上一行的结果就行了。
这样就不需要消耗太多内存。
更厉害的是:Inplace也可以,只要你每次从后往前扫描就行了。一个array也能搞定:
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> ret = new ArrayList<Integer>(); for (int i = 0; i <= rowIndex; i++) {
for (int j = i; j >= 0; j--) {
if (j == i) {
ret.add(1);
} else if (j != 0) {
// ERROR: use add instead of set
//ret.add(ret.get(j) + ret.get(j - 1));
ret.set(j, ret.get(j) + ret.get(j - 1));
}
}
} return ret;
}
}
LeetCode: Pascal's Triangle II 解题报告的更多相关文章
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- [LeetCode] 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】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- LeetCode——Pascal's Triangle II
Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...
- 【LeetCode】275. H-Index II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...
随机推荐
- JavaScript Window Location 当前页面的地址
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Location window.location 对象在编写时可不使用 window ...
- iOS 10 的一个重要更新-开发 iMessage 的第三方插件
苹果官方的 Messages 在 iOS 10 推出了非常重大的更新,可能主要是想从其他 IM 巨头手里抢点市场份额回来,包括 Facebook Messenger, Wechat 和 Snapcha ...
- Zabbix触发器支持的函数说明
原文出处:https://www.zabbix.com/documentation/2.0/manual/appendix/triggers/functions 译者: pengyao abschan ...
- 实体格式化转xml
In the past, I've done the following to control datetime serialization: Ignore the DateTime property ...
- 二进制安装mysql 5.6
创建用户和组 # groupadd mysql # useradd -r -g mysql mysql 解压压缩包 # tar -xvf mysql-5.6.37-linux-glibc2.12-x8 ...
- cucumber java从入门到精通(2)用代码定义步骤
cucumber java从入门到精通(2)用代码定义步骤 上一节里我们定义了feature文件,feature文件就是自然语言描述的用例文件,它有一定的章法,具体的潜规则是: 使用Feature关键 ...
- Android开发实现计算器的例子
例子 代码如下 复制代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" a ...
- Eclipse设置项目默认编码和换行符类型
为了实现不同操作系统间的Eclipse项目移植的一致性. 一.建议设置如下: 1. 默认的项目编码统一用UTF-8 2. 默认的换行符用UNIX类型 二.具体的配置点见下图:
- Oozie介绍
1. Hadoop常见调度框架: (1)Linux Crontab:Linux自带的任务调度计划,在任务比较少的情况下,可以使用这种方式,直接执行脚本,例如添加一个执行计划: 0 12 * hive ...
- sprintboot 发布
http://blog.csdn.net/hguisu/article/details/51072683