LeetCode119. Pascal's Triangle II
Description
Given an index k, return the kth row of the Pascal’s triangle.
For example, given k = 3,
Return [1,3,3,1].
my program
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res = {1};
for(int i = 1; i<=rowIndex; i++)
{
res.push_back(0);
for(int j = res.size(); j>0; j--)
{
res[j] += res[j-1];
}
}
return res;
}
};
34 / 34 test cases passed.
Status: Accepted
Runtime: 0 ms
other methods
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+1,1);
int small = rowIndex/2;
long comb = 1;
int j = 1;
for (int i=rowIndex; i>=small; i--){
comb *= i;
comb /= j;
j ++;
ans[i-1] = (int)comb;
ans[j-1] = (int)comb;
}
return ans;
}
LeetCode119. Pascal's Triangle II的更多相关文章
- 每天一道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, ...
- [LeetCode119]Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- 28. Triangle && Pascal's Triangle && Pascal's Triangle II
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II
118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- Pascal's Triangle,Pascal's Triangle II
一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...
- 杨辉三角形II(Pascal's Triangle II)
杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...
- LeetCode OJ 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, ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
随机推荐
- Swift中结合使用枚举与Switch
定义: 用法:
- [转] c++ try catch 问题
windhaunting,原文地址 以前都是用try{} catch(…){}来捕获C++中一些意想不到的异常, 今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的.例如下面的代码: ...
- java只有值传递,不存在引用传递
今天,我在一本面试书上看到了关于java的一个参数传递的问题: 写道 java中对象作为参数传递给一个方法,到底是值传递,还是引用传递? 我毫无疑问的回答:“引用传递!”,并且还觉得自己对java的这 ...
- javascript前端三层,字面量,变量,语句(if,switch,三元运算符,for,do while等)
1:前端三层: 结构层 HTML 样式层 CSS 行为层 JavaScript 2:JavaScript语句和语句之间的换行.空格.缩进都不敏感.alert("你");alert ...
- ylbtech-LanguageSamples-ComInteropPart2(COM 互操作第二部分)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-ComInteropPart2(COM 互操作第二部分) 1.A,示例(Sample) ...
- IP windows相关
nbtstat: 假设 我们 通过net view 获取了 局域网内一些计算机名 如上标红的计算机名称 如何 才能获取计算机的ip呢? 接下来使用 nbtstat -a 列出远程机器的名称表: ...
- pwn2own
Pwn2Own是全球最著名的黑客大赛之一,由美国五角大楼入侵防护系统供应商TippingPoint的DVLabs赞助,今年已经是第六届. 1比赛规则 参赛黑客们的目标是4大主流网页浏览器——IE.Fi ...
- C# 操作mongodb 简单实例
本实例主要简单的查询,新增,修改和删除操作,完整代码如下: using System; using System.Collections.Generic; using System.Text; usi ...
- Android Exception 17(database or disk is full)
android.database.sqlite.SQLiteFullException: database or disk is full delete some app,or clear cache
- EXTJS4自学手册——图形行为(rotate,scale)
一.旋转图像: { xtype: 'button', text: '旋转的字', handler: function (btn) { Ext.create('Ext.window.Window', { ...