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. Ecshop(二次开发) - 后台添加左侧菜单导航

    1.链接地址:修改 admin\includes\inc_menu.php 文件. $modules['17_syn_data']['view_syn']        =    'synchroni ...

  2. Web Uploader文件上传&&使用webupload有感(黄色部分)

    引入资源 使用Web Uploader文件上传需要引入三种资源:JS, CSS, SWF. <!--引入CSS--> <link rel="stylesheet" ...

  3. 在win7电脑中如何查看运行进程的PID标识符

    在介绍技巧方法之前,咱们还是先来介绍一下什么是PID标识符,这个PID标识符就是系统对运行中的程序自动分配的一个编号,是用来识别对应进程的,而且这个编号也是一一对应,不会有重复的,只有当系统结束运行的 ...

  4. CentOS修改163源(转载)

    From:http://www.linuxidc.com/Linux/2012-08/69043.htm #CentOS-Base.repo其他版本文件在http://mirrors.163.com/ ...

  5. SPR EAD NET 6

    SPR EAD_NET6 下载地址 http://www.gcpowertools.com.cn/downloads/trial/Spread.NET/EN_SPREAD_NET6_SETUP_RA_ ...

  6. 使用kerl安装erlang遇到的问题及解决办法

    1 需要安装相关包 -dev autoconf 2 出现下面错误 * documentation : * xsltproc is missing. * fop is missing. * xmllin ...

  7. 解决insmod: error inserting 'hello.ko': -1 Invalid module format

    编译自己的内核模块后,insmod出现error:error inserting 'hello.ko': -1 Invalid module format 出现这种情况的原因是因为Makefile种使 ...

  8. JAVA中的定时调度(Timer和TimerTask)

    某些时候我们需要定时去完成一些任务,这里举一个例子:我们需要在3秒钟后打印当前系统时间,此后每隔5秒重复此操作.代码如下: import java.util.TimerTask; import jav ...

  9. NYOJ 49-开心的小明:01背包

    点击打开链接 开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是 ...

  10. (medium)LeetCode 224.Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...