【一天一道LeetCode】#118. Pascal's Triangle
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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]
]
(二)解题
题目大意:求解杨辉三角
即每一行第i个数(除首尾元素等于1外),其他都等于上一行的第i-1个数和第i个数相加。
详细解释见代码注释:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ret;
int n = 1;//从第1行开始
while(n<=numRows)
{
vector<int> temp;
for(int i = 0 ; i < n ; i++)
{
if(i==0||i==n-1) temp.push_back(1);//首尾等于1
else{
temp.push_back(ret[n-2][i-1]+ret[n-2][i]);//其他的等于上一行的第i-1个加上第i个
}
}
ret.push_back(temp);
n++;
}
return ret;
}
};
【一天一道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
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 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, ...
随机推荐
- 选项卡js版封装
以下是封装函数: // id:最外边大盒的id名function tab(id,ev){ var oWrap = document.getElementById(id); ...
- 微信小程序适配iphonex
// 在app.js中判断是否是哪种设备 globalData: { isIphoneX: false, userInfo: null }, onShow:function(){ let that ...
- jquery checkbox是否选中
$("#chkDisplayZxOnly").is(":checked") 选中返回true,否则返回false
- 手把手教你全家桶之React(一)
前言 最近项目用到react,其实前年我就开始接触react,时光匆匆,一直没有时间整理下来(太懒啦)!如今再次用到,称工作间隙,对全家桶做一次总结,项目源码地址.废话不多说,上码. 创建一个文件目录 ...
- Java面试06|项目相关介绍
1.明确你的项目到底是做什么的,有哪些功能 广告投放机:项目主要是为移动端有针对性的进行广告展示. 媒体管理平台SSP:为媒体端实现多种变现途径 (1)广告投放机中关于广告检索与排序的功能 1.广告检 ...
- Node.js HTTPS
稳定性: 3 - 稳定 HTTPS 是基于 TLS/SSL 的 HTTP 协议.在 Node 里作为单独的模块来实现. 类: https.Server 这是 tls.Server 的子类,并且和 ht ...
- 关于Linux下软件包aptitude的相关操作
aptitude+回车 - 进入aptitude操作界面,可以对预览查看各种软件包 aptitude show package_name - 列出与XXX相关的软件包信息,但是并不能看到该软件包所安装 ...
- Linux文件编辑命令详细整理
刚接触Linux,前几天申请了个免费体验的阿里云服务器,选择的是Ubuntu系统,配置jdk环境变量的时候需要编辑文件. vi命令编辑文件,百度了一下,很多回答不是很全面,因此编辑文件话了一些时间. ...
- springMVC源码分析--AbstractHandlerMethodMapping注册url和HandlerMethod对应关系(十一)
在上一篇博客springMVC源码分析--AbstractHandlerMethodMapping获取url和HandlerMethod对应关系(十)中我们简单地介绍了获取url和HandlerMet ...
- 联想G510 在新的SSD上安装Win8.1系统,启动的时候自己加载机械硬盘的Win8.1系统
进入BIOS,选择Boot,将Boot Priority(优先),修改为Legacy(传统) First: 启动的时候就不会使用UEFI First的windows Boot Manager(wind ...