118. 119. Pascal's Triangle -- 杨辉三角形
118.
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
if(numRows < )
return ans;
vector<int> pre, cur;
pre.push_back();
ans.push_back(pre);
for(int i = ; i < numRows; i++)
{
cur.push_back();
for(int j = ; j < pre.size()-; j++)
{
cur.push_back(pre[j]+pre[j+]);
}
cur.push_back();
ans.push_back(cur);
pre = cur;
cur.clear();
}
return ans;
}
};
119.
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+, );
ans[] = ;
for(int i = ; i <= rowIndex; i++)
{
ans[i] = ;
for(int j = i-; j > ; j--)
{
ans[j] += ans[j-];
}
}
return ans;
}
};
只开一个数组,从后向前计算。
118. 119. Pascal's Triangle -- 杨辉三角形的更多相关文章
- 【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, ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 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. Pascal's Triangle杨辉三角形(全部/一行)
[抄题]: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- [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]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
随机推荐
- php cookie详解
各参数详解 注意: 1 当一个Cookie被删除时,它的值在当前页在仍然有效的.原因是删除cookie实际也是设置cookie, 只是把cookie的值设为‘’或者null,或者把cookie的 ...
- Excel2003命令栏操作
Excel2003个人觉得应该把它抛掉不管了,但还是没忍住想看下它里面的东东. 一.先列出一些重要对象 Commandbars:命令栏集合 ...
- SQL疑难杂症【1】解决SQL2008 RESTORE 失败问题
有时候从服务器或者其它电脑上面备份的数据库文件在还原到本地的时候会出现以下错误: 这种情况通常是备份文件之前的逻辑名称跟当前的名称对应不上,我们可以通过以下语句查看备份文件的逻辑名称: 知道备份文件 ...
- Webbrowser控件判断网页加载完毕的简单方法 (转)
摘自:http://blog.csdn.net/cometnet/article/details/5261192 一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,W ...
- 【T-SQL系列】常用函数—聚合函数
聚合函数平均值AVG.标准偏差STDEV.方差VAR.最大值MAX.最小值MIN.合计SUM.次数COUNT.极差值MAX-MIN.变异系数STDEV/AVG*100 什么是统计统计 就是通过样本特性 ...
- 四十条测试你是不是合格的PHP程序员
四十条测试你是否合格的PHP程序员,不官方,也不权威,但很给力.超过三条就不合格了.超过五条就得好好反省下自己的不足了. 1. 不会利用如phpDoc这样的工具来恰当地注释你的代码 2. 对优秀的集成 ...
- iOS - Swift SingleClass 单例类
前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 单例类的创建 1.1 单例类的创建 1 单例类的创建 ...
- [转载] Linux进程关系
在工作中, 主进程创建了子进程, 而子进程又创建了孙子进程, 然而子进程被莫名其妙的 kill 了, 结果主进程又启动了一个子进程, 子进程又尝试创建孙子进程, 但是这时候就有问题了, 因为孙子进程还 ...
- jj前端项目1th总结
1:设计图--->分出几个独立模块--->颗粒化布局--->文档流控制整体布局--->固定位置的元素绝对定位,段落这种元素不可绝对定位.----->加上和后台交互用的js ...
- Android notifications通知栏的使用
app发送通知消息到通知栏中的关键代码和点击事件: package com.example.notifications; import android.os.Bundle; import androi ...