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的更多相关文章

  1. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  2. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  3. 每天一道LeetCode--118. Pascal's Triangle(杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. Java实现 LeetCode 119 杨辉三角 II

    119. 杨辉三角 II 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...

  5. LeetCode119.杨辉三角 II

    119.杨辉三角 II 描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 3 输出: [1,3,3,1] 进阶 ...

  6. [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 ...

  7. 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...

  8. [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, ...

  9. LeetCode 118:杨辉三角 II Pascal's Triangle II

    公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...

随机推荐

  1. unicode转换为中文

    unicode转换为中文 \u5f53\u5730\u65f6\u95f42019\u5e747\u670813\u65e5\uff0c\u82f1\u56fd\u8d1d\u5fb7\u798f\u ...

  2. 10. vue-router命名路由

    命名路由的配置规则 为了更加方便的表示路由的路径,可以给路由规则起一个别名, 即为"命名路由". const router = new VueRouter ({ routes: [ ...

  3. Python微信操控(itchat)

    itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单. 开源地址 https://github.com/littlecodersh/ItChat 文档: https://itc ...

  4. 基于思岚A1激光雷达+OpenGL+VS2017的Ramer-Douglas-Peucker算法的实现

    时隔两年 又借到了之前的那个激光雷达,最老版本的思岚A1,甚至不支持新的固件,并且转接板也不见了,看了下淘宝店卖¥80,但是官方提供了一个基于STM32的实现方式,于是我估摸着这个转接板只是一个普通的 ...

  5. 【转载】Visual Studio Code 构建 C/C++ 开发环境

    https://www.cnblogs.com/XieSir/articles/8288051.html 1. 安装 MinGW Distro / MinGW / GNU GCC 中的任何一款,( W ...

  6. linux patch 简单学习

    使用patch 我们可以方便的进行软件补丁包处理,以下演示一个简单的c 项目补丁处理 原代码 app.c #include <stdio.h> int main(){ printf(&qu ...

  7. ping fping

    通过ping来监测对端网络状态 ping fpinf在windows和linux上的参数是不同的,返回的结果也是不同的 在网络连通性监测方面用的比较多,在py go中调用命令,对返回的结果使用正则来在 ...

  8. 杂乱的Solidity - 2019-7-13

    要清楚在区块链上开发DApp的架构[x][][][][][]   DApp是去中心化的应用   基于智能合约 去中心化的游戏规则 代币激励  

  9. LG4074【WC2013】糖果公园 【树上莫队,带修莫队】

    题目描述:给出一棵 \(n\) 个点的树,点有颜色 \(C_i\),长度为 \(m\) 的数组 \(V\) 和长度为 \(n\) 的数组 \(W\).有两种操作: 将 \(C_x\) 修改为 \(y\ ...

  10. 洛谷P2331[SCOI2005]最大子矩阵

    题目 DP 此题可以分为两个子问题. \(m\)等于\(1\): 原题目转化为求一行数列里的\(k\)块区间的和,区间可以为空的值. 直接定义状态\(dp[i][t]\)表示前i个数分为t块的最大值. ...