Pascal's Triangle,Pascal's Triangle II
一.Pascal's Triangle
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>> res;
if(numRows==){
return res;
}
vector<int> row;
int size = ;
while(numRows--){
int x = 1;for(int i=;i<size;i++){
int y = row[i];
row[i]= x+y;
x = y;
}
row.push_back();
res.push_back(row);
size++;
}
return res;
}
};
二.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]
.
class Solution {
public:
vector<int> getRow(int rowIndex) {
rowIndex+=;
vector<int> row;
int size = ;
while(rowIndex--){
int x = ;
for(int i=;i<size;i++){
int y = row[i];
row[i] = x+y;
x=y;
}
row.push_back();
size++;
}
return row;
}
};
Pascal's Triangle,Pascal's Triangle II的更多相关文章
- 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, ...
- LeetCode Pascal's Triangle && Pascal's Triangle II Python
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
- LeetCode Pascal's Triangle Pascal三角形
题意:给一个数字,返回一个二维数组,包含一个三角形. 思路:n=0.1.2都是特例,特别处理.3行以上的的头尾都是1,其他都是依靠上一行的两个数.具体了解Pascal三角形原理. class Solu ...
- pascal+sublime搭建Pascal学习环境
一.fpc安装 1. 下载:http://www.freepascal.org/down/i386/win32.var(或者:http://download.csdn.net/detail/wenph ...
- 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] 2. Pascal's Triangle II
我是按难度往下刷的,第二道是帕斯卡三角形二.简单易懂,题目如下: Given an index k, return the kth row of the Pascal's triangle. For ...
- leetcode 【 Pascal's Triangle II 】python 实现
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- leetcode—pascal triangle
1.题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
随机推荐
- SQL Server2008R2安装失败问题之语言包问题
今天安装SQL Server2008 的时候出现了,如下的的问题,安装过程在ExcuteStandardTimingsWorkflow时候报错,结束安装. 提示: ...
- web前端的学习误区
web前端的学习误区 网页制作是计算机专业同学在大学期间都会接触到的一门课程,而学习网页制作所用的第一个集成开发环境(IDE)想必大多是Dreamweaver,这种所见即所得的“吊炸天”IDE为我们 ...
- MSDTC问题集
一.链接服务器的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务. 尊重原著作:本文转载自http://sfwxw456.blog.163.com/blog/sta ...
- MYSQL 注释的 3 方法
方法 1.单行注释 # 方法 2.单行注释 -- 方法 3.多行注释 /*------*/ ------------------------------------------------------ ...
- MFC可执行文件问题
MFC生成的.exe可执行文件,在其它机子上无法正常执行.主要是MFC库链接方式的问题,使用MFC分动态连接和静态连接两种: 静态连接就是把需要的MFC库函数放进你的exe之中,这样,在MFC库函 ...
- 基于TCP套接字实现的简单Demo
由于代码的注释已经很详尽了,所以这里不再作过多说明.仅仅贴出代码和结果图. 值得注意的是必须先启动server程序再启动client. Server: #include <WINSOCK2.H& ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- 《Java解惑》书摘
例子1:关于char数组的输出 System.out.println("H" + "a");//输出:Ha System.out.println('H' + ' ...
- Linux 零碎知识点
ln -s ../libs/ libs 在当前目录下建立一个符号链接文件libs,使它指向上一层目录的libs文件夹 关于su和su -的区别切换用户是可以使用su tom或者su - tom来实现, ...
- poj 2531 Network Saboteur(经典dfs)
题目大意:有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值. 思路:1.把这两个集合标记为0和1,先默认所有点都在集合0里. 2 ...