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 ...
随机推荐
- ARM处理器启动流程
根据<<芯片手册>>查看相关内容: 1.启动方式 2.地址布局 3.启动流程
- SQL语句最基本的性能优化方法
有些人还不知道sql语句的基本性能优化方法,在此我简单提醒一下,最基本的优化方法: 1.检查是否缺少索引.调试的时候开启“包括实际的执行计划” 执行后会显示缺少的索引, 然后让dba帮助添 ...
- 9.Methods(二)
4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...
- HDU 5877 Weak Pair(弱点对)
HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Jav ...
- Binding的源与路径
1.把控件作为Binding的源 例子:拖动Slider,输入框中的值也会跟着改变,或在输入框中输入数值,滑动条也会自动移动 <Window x:Class="把控件作为Binding ...
- 关于ListView嵌套GridView中的onItemClickListener失效问题
一开始在ListView中设置了onItemClickListener,在里面Log输出Item列表的位置,完全没有反应, 网上大部分说的什么把子组件屏蔽掉(而且好多都是转载的一样的), 可是我希望的 ...
- 你未必知道的css小知识
1:当按百分比设定一个元素的宽度时,它是相对于父容器的宽度计算的,但是,对于一些表示竖向距离的属性,例如padding-top,padding-bottom,margin-top,margin-bot ...
- Doragon Kuesuto 1.15
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() { ; ; ; int actio ...
- 简单的php性能注意点
什么情况,可能遇到性能问题: 1.php语法使用的不恰当 2.使用php语言做了它不擅长做的事 3.用php语言连接的服务不给力 4.php自身的短板 5.我也不知道的问题 一般情况:php性能问题不 ...
- Python学习(16)File(文件)方法
Python File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读 ...