[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,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
118. Pascal's Triangle 的拓展,给一个索引k,返回杨辉三角的第k行。
解法:题目要求优化到 O(k) 的空间复杂,那么就不能把每行都记录下来,而只是记录前一行和当前行。
Java:
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
int curr[] = new int[rowIndex + 1];
int prev[] = new int[rowIndex + 1];
prev[0] = 1;
for(int row = 1; row <= rowIndex; row++) {
curr[0] = 1;
curr[row] = 1;
for(int i = 1; i < row; i++)
curr[i] = prev[i] + prev[i - 1];
int[] swap = curr;
curr = prev;
prev = swap;
}
for(int i = 0; i <= rowIndex; i++)
res.add(prev[i]);
return res;
}
}
Java:
public class Solution {
public List<Integer> getRow(int rowIndex) {
ArrayList<Integer> row = new ArrayList<Integer>();
for (int i=0; i<rowIndex+1; i++){
row.add(0,1);
for(int j=1; j<row.size()-1;j++){
row.set(j, row.get(j)+row.get(j+1));
}
}
return row;
}
}
Python:
class Solution:
# @return a list of integers
def getRow(self, rowIndex):
result = [0] * (rowIndex + 1)
for i in xrange(rowIndex + 1):
old = result[0] = 1
for j in xrange(1, i + 1):
old, result[j] = result[j], old + result[j]
return result
C++:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> out;
if (rowIndex < 0) return out;
out.assign(rowIndex + 1, 0);
for (int i = 0; i <= rowIndex; ++i) {
if ( i == 0) {
out[0] = 1;
continue;
}
for (int j = rowIndex; j >= 1; --j) {
out[j] = out[j] + out[j-1];
}
}
return out;
}
};
C++:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> result(rowIndex+1, 0);
result[0] = 1;
for(int i=1; i<rowIndex+1; i++)
for(int j=i; j>=1; j--)
result[j] += result[j-1];
return result;
}
};
类似题目:
[LeetCode] 118. Pascal's Triangle 杨辉三角
All LeetCode Questions List 题目汇总
[LeetCode] 119. Pascal's Triangle II 杨辉三角 II的更多相关文章
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 每天一道LeetCode--118. Pascal's Triangle(杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Java实现 LeetCode 119 杨辉三角 II
119. 杨辉三角 II 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...
- LeetCode119.杨辉三角 II
119.杨辉三角 II 描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 3 输出: [1,3,3,1] 进阶 ...
- [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】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...
- [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, ...
- LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...
随机推荐
- linux的后台运行相关命令
screen -S name 创建一个名为name的后台,或者说bash面板,在这上面运行的任务不会因为连接断开而退出,且保留bash上的信息 screen -ls 列出所有的screen scree ...
- jvm 堆栈概念
关于JVM的工作原理以及调优是一个向往已久的模块,终于有幸接触到:http://pengjiaheng.iteye.com/blog/518623 那就顺着这个思路,来梳理一下自己看到后的结论和感想. ...
- Mycat 全局系列号(转载)
概述 本篇文章介绍mycat怎样在分库分表的情况下保证主键的全局唯一方法,接下来就来分析三种方法各自的优缺点. 配置 文件方式获取 1.修改server配置文件 vim server.xml < ...
- 持续集成学习8 jenkins权限控制
一.总体配置 1.系统管理---> Configure Global Security 2.配置基于角色授权 创建角色 ----> 分配角色 代表着所有以dev-开头的 job全部都分配给 ...
- learning java 重定向标准输入输出
output redirectionOut: public class RedirectOut { public static void main(String[] args) throws File ...
- Kapitan 通用terraform&& kubernetes 配置管理工具
Kapitan 是一个通用的配置管理工具,可以帮助我们管理terraform .kubernetes 以及其他的配置. Kapitan 自生基于jsonnet 开发,对于我们日常进行软件的部署(tf以 ...
- CSS文本元素
一.属性 font-size:16px; 文字大小 Font-weight: 700 ; 值从100-900,文字粗细,不推荐使用font-weight:bold; Font-family:微软 ...
- [USACO09DEC] Dizzy Cows 拓扑序
[USACO09DEC] Dizzy Cows 拓扑序 先对有向边跑拓扑排序,记录下每个点拓扑序,为了使最后的图不存在环,加入的\(p2\)条无向边\(u,v\)必须满足\(u\)拓扑序小于\(v\) ...
- BZOJ 1213: [HNOI2004]高精度开根
二次联通门 : BZOJ 1213: [HNOI2004]高精度开根 正解 NTT+高精+倍增+二分 但是可以用python 2333333 m,n=int(raw_input()),int(raw_ ...
- 用jquery做一个带导航的名单列表
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...