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, ...
随机推荐
- Flowerpot(又是尺取。。)
题目:http://172.21.85.56/oj/exercise/problem?problem_id=21568 题目大意:老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到 ...
- Codeforces 277E
按边建模,二叉树一条入边两条出边 判断就要用到mcmf的好处了 #include<bits/stdc++.h> using namespace std; const int maxn = ...
- linux 中varnish服务
一.安装varnish在server1中安装两个包varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm1.在server ...
- 斑马打印机客户端GET和POST,以及后端两种打印方式。
斑马打印机客户端GET和POST,以及后端两种打印方式. 背景环境:打印机安装在客户端外网.当用户登录时,通过ajax取服务器数据,返回打印机命令,然后客户端通过JS发送给斑马打印机. 1.使用Get ...
- layer 弹出对话框 子父页面相互参数传递
转载:https://blog.csdn.net/flybridy/article/details/78610737
- vue.js vue-jsonp解决跨域问题
安装jsonp npm install vue-jsonp --save main.js中引入 import VueJsonp from 'vue-jsonp' Vue.use(VueJsonp) 组 ...
- py---------面向对象进阶
一.isinstance 和 issubclass isinstance(obj,cls)检查obj是否是类cls的对象,是则返回True class Foo(object): pass obj = ...
- eclipse_project
转!java web项目 build path 导入jar包,tomcat启动报错 找不到该类 在eclipse集成tomcat开发java web项目时,引入的外部jar包,编译通过,但启动tomc ...
- 使用media query 来实现响应式设计
你的网页在手机上显示效果可以在电脑上一样好看.完成这个任务的奥秘被称为响应式设计,媒体查询(media query)是实现网页响应的关键. 在电脑上一个例子: <div class=" ...
- linux进程间的通信之 共享内存
一.共享内存介绍 共享内存是三个IPC(Inter-Process Communication)机制中的一个. 它允许两个不相关的进程访问同一个逻辑内存. 共享内存是在两个正在进行的进程之间传递数据的 ...