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,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

输出Pascal三角形的第k行(从0开始);空间复杂度为O(k);

从第0行开始生成,每次利用已经生成的行,在原有空间上生成下一行,直到生成所要求的行。

代码入下:

 class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> pascal;
pascal.push_back();
int m = ;
for(int i = ; i <= rowIndex; i++)
{
if(rowIndex == )
{
return pascal;
}
for(int j = ; j < i+; j++)
{
if(j == i)
{
pascal.push_back();
break;
}
int n = pascal[j];
pascal[j] = m + n;
m = n;
}
}
return pascal;
}
};

leetcode 119的更多相关文章

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

  2. [LeetCode 119] - 杨辉三角形II(Pascal's Triangle II)

    问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O(k)的额外空间吗? 初始思路 首先来复习复习杨辉三角形的性质 ...

  3. 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, ...

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

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

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

  6. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

  7. Leetcode 119 Pascal's Triangle II 数论递推

    杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...

  8. leetcode 119 Pascal's Triangle II ----- java

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  9. Java [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. 安装Android SDK和ADT步骤和遇到的问题

    http://894503895.diandian.com/post/2012-05-16/18695648 1.安装eclipse.下载地址:http://www.eclipse.org/downl ...

  2. rand srand

    题外:先定义一个指针变量int *a; 再将整数b的地址赋给指针变量 a=&b ;    谨记指针变量a只是地址 *a相当于整数 之后*a 就可以表示 指向b了 也可以在定义的时候初始化 in ...

  3. 30天轻松掌握JavaWeb_使用beanutils

    导入commons-beanutils-1.8.3.jar及commons-logging-1.1.3.jar 使用commons-beanutils-1.8.3.jar包时需要同时使用commons ...

  4. jsp页面编译成Servlet类文件

    package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.js ...

  5. [Java] 匿名内部类

    package test.file; import java.io.File; import java.io.FilenameFilter; /** * 匿名的内部类 * @author Frost. ...

  6. 你可能不知道的 30 个 Python 语言的特点技巧

        列表按难度排序,常用的语言特征和技巧放在前面. 1.1   分拆 >>> a, b, c = 1, 2, 3>>> a, b, c(1, 2, 3)> ...

  7. SQL Server2008 TIME类型

    SQL Server2008现在有了一个TIME数据类型,它允许你只存储一个时间值而没有时间.如果想要存储一个特定的时间信息而不涉及具体的日期时,这将非常的有用.TIME数据类型存储使用24小时制,它 ...

  8. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  9. 蓝桥杯--- 历届试题 大臣的旅费 (DFS & Vector)

    题目提交链接:http://lx.lanqiao.org/problem.page?gpid=T32 问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国 ...

  10. Package 'DXCore for Visual Studio' has failed to load properly

    Since installing 13.1  I get Package 'DXCore for Visual Studio' has failed to load properly error wh ...