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 ...
随机推荐
- [转载]We Recommend a Singular Value Decomposition
原文:http://www.ams.org/samplings/feature-column/fcarc-svd Introduction The topic of this article, the ...
- Android中AlertDialog对话框禁止按[返回键]或[搜索键]
alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(Di ...
- DiskGenius(磁盘分区/数据恢复) 32位 V4.9.1 免费绿色版
软件名称: DiskGenius(磁盘分区/数据恢复) 32位 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 19.5MB 图片预览: 软件简介: Dis ...
- spring、struts整合
package com.hanqi.test; public class JISQ { public double add(double a,double b) { return (a+b); } } ...
- python绝技 — 用Scapy测试无线网卡的嗅探功能
代码 #!/usr/bin/python #--*--coding=utf-8--*-- from scapy.all import * def pktPrint(pkt): if pkt.hasla ...
- [UWP小白日记-3]记账项目-1
学了一段时间的UWP,来个项目试试手. 本来是想边做边学MVVMLight的结果感觉MVVM对于萌新来说太高难,以后再把这个项目改造成MVVMLight框架的项目. 下面进入正题. 中间那快空白打算放 ...
- teyi
$arr=array('haha'=>"苹果");print_r($arr['haha']); $arr=array(0=>"苹果");$arr=a ...
- 基于html5 canvas 的强大图表插件【Chart.js】
名词解释 Chart.js:是基于html5和canvas的强大图表插件,支持多样的图表形式,柱状线性饼环极地雷达等等: canvas:只兼容到IE9 excanvas.js:强大的第三方兼容插件,可 ...
- PIE 阻断回溯——Cut
PIE(Prolog Inference Engine)通常是搜索所有的解.举个例子, 当然dialog窗口中一开始调用 run. 只会显示一个解(虽然事实上会得到两个解),在前面加上 X=1,就可以 ...
- 【转】如何将ACCESS数据库后缀名accdb改为mdb
office 2007中的ACCESS数据库保存时,默认的后缀名是*.accdb, 但是在数据库编程中,用到的后缀名却是*.mdb, 不能直接将后缀名由 accdb 改为 mdb,虽然没有出错,但是编 ...