119. Pascal's Triangle II (Amazon) from leetcode
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.

In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 3
Output: [1,3,3,1]
Follow up:
Could you optimize your algorithm to use only O(k) extra space?
Solution: only using O(k), update the elements in the array, we need temp to keep the previous element
using list to set : list.set(index, num);
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
res.add(1);
for(int i = 1; i<=rowIndex; i++){
int temp = res.get(0);//previous element
for(int j = 1; j<i; j++){
int k = res.get(j);
res.set(j, k+temp);
temp = k;
}
res.add(1);
}
return res;
}
}
119. Pascal's Triangle II (Amazon) from leetcode的更多相关文章
- 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- ...
- LeetCode OJ 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, ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- [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 ...
- 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]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- 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 [1,3, ...
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
随机推荐
- SHA_1计算消息摘要
/** * SHA_1计算消息摘要 * @param bytes 待计算数据 * @return */ public static String SHA_1(byte[] bytes) { Strin ...
- javaEE 转发 和 重定向
转发 两种方法: 1,request.getRequestDispatcher(getServletContext().getContextPath() + "/path").fo ...
- thinkphp5 composer安装验证码
1,安装composer,选择安装到的php的版本.在使用phpstudy的时候 用的是php5.5 .注意phpstudy的安装路径. 2.检查composer是否安装成功.cmd 然后输入comp ...
- jgroups-raft
要求 Java 8 支持 实现服务器的动态添加和删除 文档的编写 基于复制计数器的共识算法实现 https://github.com/belaban/jgroups-raft
- 转 oracheck
转 https://dbaplus.cn/news-10-220-1.html ORAchk 之前被称为RACcheck,后来它的检查范围进行了扩展,改名为了ORAchk,它是在数据库系统进行健康检查 ...
- 转 Python多版本管理-pyenv
#######for linux https://www.cnblogs.com/saneri/p/7642316.html 经常遇到这样的情况: 系统自带的Python是2.x,自己需要Python ...
- python django 基本测试
http://www.runoob.com/django/django-model.html django-admin startapp TestModel /models.py from djang ...
- JavaSE---NIO
1.概述 1.1 BIO的问题: 读取输入流中的数据时,如果没有读到有效数据,该线程将被阻塞: 传统输入.输出流都是阻塞式的: 传统的输入.输出流都是通过字节移动处理: 面向流的输入.输出一次只能处理 ...
- Notepad++实用插件
说明: Converter: 编码转换工具,ascii2hex,hex2ascii DoxyIt: 添加文件或者函数注释比较方便 DSpellCheck: 拼写检查 MIME Tools: 将Base ...
- C# 连接 Exchange 发送邮件
C#连接Exchange 发送邮件代码如下 /// <summary> /// exchange群发邮件 /// </summary> /// <param name=& ...