【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,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
题解:简单的模拟题,每次用answer列表存放上一层的值,用temp列表存放当前层的值,只要计算好temp中需要重新计算的元素的索引范围[1,i-1](第i层),然后根据answer计算就可以了,每次计算完一层更新answer为这一层的数,最后answer中存放的就是答案。
代码如下:
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> answer = new ArrayList<Integer>();
answer.add(1);
for(int i = 1;i <= rowIndex;i++){
List<Integer> temp = new ArrayList<Integer>();
temp.add(1);
for(int j = 1;j <= i-1;j++){
temp.add(answer.get(j-1)+answer.get(j));
}
temp.add(1);
answer = temp;
}
return answer;
}
}
【leetcode刷题笔记】Pascal's Triangle II的更多相关文章
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode刷题笔记】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【LEETCODE】34、119题,Pascal's Triangle II
package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
随机推荐
- js浅度克隆/深度克隆
首先弄明白几个概念: 一. 具体数据类型分为两种: 1.原始数据类型 2.引用数据类型 原始数据类型存储的是对象的实际地址,包括: number.string.boolean.还有两个特殊的nul ...
- 常见UI组件的一个模板
效果: 代码: using UnityEngine; using UnityEditor; using System.Collections; using System.IO; using Unity ...
- PHP中foreach用法详细讲解
1.foreach是什么? foreach是PHP的一种语法结构,其实就是一个工具,(工具:就是工作的时候用到的器具),那么在程序开发过程中,为了达到程序效果,就用到了foreach. 2.如何用? ...
- mysql慢查询日志分析工具(python写的)
D:\NormalSoftware>python mysql_filter_slow_log.py ./mysql1-slow.log --no-duplicates --sort-avg-qu ...
- python 迭代器,生成器与推导式
函数的动态传参 *args 动态接收所有位置参数 **kwargs 动态接收关键字参数 顺序: 位置参数, *args, 默认参数, **kwargs def func(*args, **kwargs ...
- POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- csv .xlsx
def gen_file_data(fodir, fname, sheet_index=0, ): if fname.find('.xlsx') > -1: fname_open = '%s\\ ...
- HTML 与 SGML关系
HTML :超文本标记语言,“超文本”就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素 SGML:标准通用标记语言 HTML 是SGML下的一个应用
- python webserver客户端
1.库 suds库,只能做webserver客户端,轻量化,使用方便.安装使用pip. 2.使用 如有webserver情况如下: url:http://10.110.35.41:8980/wsser ...
- QT设置QToolBar带有图标和文字
ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);