LeetCode 118 Pascal's Triangle
Problem:
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]
]
Summary:
输出杨辉三角的前n行。
Solution:
方法类似于LeetCode 119 Pascal's Triangle II
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
vector<int> v;
for (int i = ; i < numRows; i++) {
v.resize(i + );
v[] = v[i] = ;
for (int j = i - ; j > ; j--) {
v[j] = v[j - ] + v[j];
}
res.push_back(v);
}
return res;
}
};
class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> res; vector<int> v; for (int i = 0; i < numRows; i++) { v.resize(i + 1); v[0] = v[i] = 1; for (int j = i - 1; j > 0; j--) { v[j] = v[j - 1] + v[j]; } res.push_back(v); } return res; }};
LeetCode 118 Pascal's Triangle的更多相关文章
- 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#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- leetcode 118 Pascal's Triangle ----- java
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Leetcode 118 Pascal's Triangle 数论递推
杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...
- [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, ...
随机推荐
- tomcat远程部署应用
Tomcat安装成功后,在ip地址:8080上就可以看见熟悉的首页,在这个首页中,上方有一个manage app按钮,点击就可以进行应用管理了.这样就不需要使用ftp把war包传上去了. 要想远程部署 ...
- Java 中 手动抛出异常: throw new Exception("错误信息") 错误信息的获得
当然需要先用try catch捕获,但注意new Exception("")括号里的字符串其实是异常原因,所以获取是要用ex.getCause().getMessage() int ...
- 通过form上传文件(php)
前段代码 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- 教你一招:解决u盘插入计算机时提示格式化,如何恢复u盘中的文件
1.插入U盘时,计算机提示格式化 看到这里,到底是格不格呢?别怕,随便你了. 2.查看U盘属性,发现都为零 怎么办呢?u盘上面有很多重要文件啊!别急,继续往下看. 3.解决办法 (1)下载DiskGe ...
- 仿QQ大战—服务器的搭建(ServerSocket)
ServerSocket(服务器): ServerSocket是JAVA中提供的用于建立服务器的类: 在客户/服务器通信模式中, 服务器端需要创建监听端口的 ServerSocket, ServerS ...
- iOS开发UI篇—懒加载
iOS开发UI篇—懒加载 1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了, ...
- 加载plist文件数据的方法
这个pilist文件最外面的是一个数组,数组中每一个item是一个字典,我们的目的就是为了取到每一个item字典中的内容数据 下面看代码举例 //加载数组 - (void)handleData { / ...
- LINQ驱动数据的查询功能
一.LINQ概念 LINQ是微软在.NetFramework3.5中新加入的语言功能,在语言中以程序代码方式处理集合的能力. 1.1 LINQ VS 循环处理 在我刚工作时候,对于集合对象的处理一般是 ...
- 深入理解javascript原型和闭包(17)——补this
本文对<深入理解javascript原型和闭包(10)——this>一篇进行补充,原文链接:http://www.cnblogs.com/wangfupeng1988/p/3988422. ...
- rails 常用的知识点
按惯例先上网址: http://guides.ruby-china.org/ 适合初学者很好的文章 ===========================知识点================ ...