leetcode 119
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的更多相关文章
- [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)
问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O(k)的额外空间吗? 初始思路 首先来复习复习杨辉三角形的性质 ...
- 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 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Java实现 LeetCode 119 杨辉三角 II
119. 杨辉三角 II 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...
- 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 ...
- 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: ...
- 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, ...
- 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 ...
随机推荐
- rand()随机数的产生
#include "stdio.h"#include<stdlib.h>用下列公式即可得到指定范围[m,n]的随机数: r = rand()%(n - m + 1) + ...
- 获取mysql数据表中的列名
select COLUMN_NAME from information_schema.columns where table_name='table_name' DESCRIBE table_name ...
- 火狐firefox提示“内容编码错误 无法显示您尝试查看的页面,因为它使用了无效或者不支持的压缩格式。”
火狐firefox浏览器打开网也是时提示“内容编码错误 无法显示您尝试查看的页面,因为它使用了无效或者不支持的压缩格式.” 今早一来打开用PHPCMS做的网站时就提示这个错误,用其他浏览器打开提示的是 ...
- 【jQuery】关于选择器中的 :first 、 :first-child 、 :first-of-type
[:first] <!DOCTYPE html><html lang="zh-CN"><head> <title>test&l ...
- rubymine配置 rspec
If you go in Run > Edit Configurations... And select All Specs in <Your Project> you'll see ...
- ruby1.8到2.1语法改变
1定义hash的语法改变old_way = {:foo => "bar", :one => 1}new_way = {foo: "bar", one ...
- rsyncd.conf 文件
uid = nobodygid = nobodymax connections = 10timeout = 60use chroot = noread only = falsepid file=/va ...
- 九度OJ1008
这道题其实就是一个简单的Dijkstra变形,只是要考虑重边的情况. 当在更新图中点到起点的距离时,将花费p也计算在内:如果长度与之前计算的值相等,则再考虑此时花费p是否会更少,是的话则仍然要更新最短 ...
- Mingyang.net:hibernate.hbm2ddl.auto配置详解【转】
原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hibernate.cfg.xml 中hibern ...
- 视频相关android软件
1. 视频解码工具:ffmpeg, http://www.ffmpeg.org/ 2. java有一个开源程序: yoyoPlayer, 可以到这个代码中去学习相关的音频知识.http://www.b ...