LeetCode OJ 119. Pascal's Triangle II
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,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
【思路】
一刷:我们为了满足空间复杂度的要求,我们新建两个ArrayList,一个负责存储上一个Pascal行的结果,一个根据上一个Pascal行得出当前Pascal行的结果,这个过程循环进行。
二刷:ArrayList的操作是比较慢的,这次我们用数组来对结果进行存储。数组保存上一个状态,通过上一个状态计算下一个状态。例如:[1,3,3,1],我们可以从最后一个元素开始,num[i] = num[i] + num[i-1]。最后我们再在末尾补1。这样能大大提高代码的效率。
【java代码一刷】
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> prelist = new ArrayList<>();
prelist.add(1);
if(rowIndex <= 0) return prelist;
List<Integer> curlist = new ArrayList<>();
for(int i = 1; i <= rowIndex; i++){
curlist.add(1);
for(int j = 0; j < prelist.size() - 1; j++){
curlist.add(prelist.get(j) + prelist.get(j+1));
}
curlist.add(1);
List<Integer> temp = prelist;
prelist = curlist;
curlist = temp;
curlist.clear();
}
return prelist;
}
}
【java代码二刷】
public class Solution {
public List<Integer> getRow(int rowIndex) {
int[] nums = new int[rowIndex+1];
for(int i = 0; i <= rowIndex; i++) {
for(int j = i; j >= 1; j--)
nums[j] += nums[j-1];
nums[i] = 1;
}
List<Integer> res = new ArrayList<>(rowIndex);
for(int num : nums) res.add(num);
return res;
}
}
LeetCode OJ 119. Pascal's Triangle II的更多相关文章
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】119. Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- Leetcode No.119 Pascal's Triangle II(c++实现)
1. 题目 1.1 英文题目 Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's tria ...
- 【LeetCode OJ】Pascal's Triangle II
Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...
- 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/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- 119. Pascal's Triangle II@python
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
随机推荐
- linux标准输入输出2>&1
linux中有三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字是0,1,2. STDIN是标准输入,默认从键盘读取信息:STDOUT是标准输出,默认将输出结果输出至终 ...
- python3中字典的copy
字典是可变的: first和second同时指向一个字典.first修改也会影响second.在程序中一定注意对字典参数的修改会对原始的字典进行修改.这也体现了字典是可变的. 字典的copy方法是浅拷 ...
- 破译情报-NOIP2016提高组复赛模拟试题
[题目描述] 最近国安人员截获了一份 RB 国的秘密情报, 全文都是经过加密的,每个单 词都很长.破译人员想到先把单词化简一下,方法是把每个单词尽量取短些的前 缀,但所取的前缀不能是其他单词的前缀. ...
- The APR based Apache Tomcat Native library tomcat启动错误
The APR based Apache Tomcat Native library which allows optimal performance in production environmen ...
- bind() unbind()绑定解绑事件
.bind( eventType [, eventData], handler(eventObject)) 本文实例分析了JQuery中Bind()事件用法.分享给大家供大家参考.具体分析如下: .B ...
- 常用的html标签大全
html标签大全 一.文字 1.标题文字 <h#>..........</h#> #=1~6:h1为最大字,h6为最小字 2.字体变化 <font>........ ...
- 敏捷开发(八)- Scrum Sprint计划会议1
本文主要是为了检测你对SCRUM Sprint 计划会议的了解和使用程度, 通过本文你可以检测一下 1.你们的SCRUM Sprint 计划会议的过程和步骤 2.会议的输出结果 S ...
- 《Intel汇编第5版》 汇编逆转字符串
一.逆转字符串 逆转一个字符串可以利用栈这个数据结果,顺次读取所有元素压栈,再出栈所有元素即可逆序 二.push和pop指令 三.pushfd和popfd 四.pushad和popad 五.代码以及结 ...
- NVIC
1中断:每一个中断都会对应一个服务程序 2NVIC 他的做用是负责中断优先级管理 3IP bit[7:4]每一个中断都有一个IP寄存器,用于设置中断相关的属性 AIRCR[10:8]只一个AIRCR寄 ...
- SAP HANA 创建属性视图
[Step By Step]SAP HANA创建属性视图(Attribute View) Demo Instruction: 从一张用户信息表中组合出相信地址. 1. 在modeler窗口中,找到相应 ...