【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刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
随机推荐
- R语言数据分析系列之四
R语言数据分析系列之四 -- by comaple.zhang 说到统计分析我们就离不开随机变量,所谓随机变量就是数学家们为了更好的拟合现实世界的数据而建立的数学模型.有了她我们甚至能够来预測一个站点 ...
- Android 与H5之间的js交互
之前项目做过一些Android和Html5之间js交互方面的东西,今天有时间就总结一下: 一.为什么要进行js交互: 为了方便原生开发和Html之间数据传递,在静态页面的情况下可以改变原生开发的页面: ...
- java高级主题
1 java.util.concurrent.locks.LockSupport park:阻塞线程. unpark:解除阻塞线程. 线程阻塞最基础的组件. 2 sun.misc.Unsafe 可以用 ...
- Linux学习路线指南
转载的,感觉写的挺好的,我自己知识复制了下,忘记了转载地址,抱歉! Linux学习路线指南 很多同学接触Linux不多,对Linux平台的开发更是一无所知.而现在的趋势越来越表明,作为一个优秀的软件开 ...
- python+NLTK 自然语言学习处理七:N-gram标注
在上一章中介绍了用pos_tag进行词性标注.这一章将要介绍专门的标注器. 首先来看一元标注器,一元标注器利用一种简单的统计算法,对每个标识符分配最有可能的标记,建立一元标注器的技术称为训练. fro ...
- 如何使用Django实现用户登录验证
最初开始搞用户登录验证的时候感觉没什么难的,不就是增删改查中的查询数据库么,但是还是遇到许多小问题,而且感觉在查询数据库的时候,要把前端的数据一条一条的进行比对,会导致我的代码很丑,而且方式很不智,所 ...
- KVM虚拟化技术实战全过程
今天准备开始.................... centos安装-kvm 教程: http://www.linuxidc.com/Linux/2017-01/140007.htm http:// ...
- secureCRT linux shell显示中文乱码 解决方法
引:有没有这样的经历: 1.在shell中直接查看包含中文的文件时,出现一堆火星文,不得不下载下来window看. 2.无法正常的在shell中输入中文. 3.make的时候输出一堆乱码. 以下是查阅 ...
- 禁用chrome浏览器的cookie
Chrome: 1.打开chrome浏览器,点击右上角的“自定义和控制Google Chrome”按钮 2.在下拉菜单中选择设置 3.点击设置页底部的“显示高级设置...” 4.在隐私设置下,点击“内 ...
- HackerRank - beautiful-binary-string 【字符串】
题意 给出一个 N 位的 01 串 然后 每次 改动 可以将其中的 (0 -> 1) 或者 (1 -> 0) 然后 求 最少几次 改动 使得 这个 01 串 当中 不存在 连续的 010 ...