【leetcode】118. Pascal's Triangle
@requires_authorization
@author johnsondu
@create_time 2015.7.23 19:54
@url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
/************************
* @description: simple.
* @time_complexity: O(n)
* @space_complexity: O(n)
************************/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
for(int i = 1; i <= numRows; i ++) {
vector<int> layer;
if(i == 1) layer.push_back(1);
else {
for(int j = 1; j <= i; j ++) {
if(j == 1 || j == i) layer.push_back(1);
else layer.push_back(ans[i-2][j-2] + ans[i-2][j-1]);
}
}
ans.push_back(layer);
}
return ans;
}
};
@requires_authorization
@author johnsondu
@create_time 2015.7.23 19:54
@url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
/************************
* @description: simple.
* @time_complexity: O(n)
* @space_complexity: O(n)
************************/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
if(numRows < 1) return ans;
vector<int> first;
first.push_back(1);
ans.push_back(first);
if(numRows < 2) return ans;
vector<int> second;
second.push_back(1);
second.push_back(1);
ans.push_back(second);
for(int i = 3; i <= numRows; i ++) {
vector<int> layer;
layer.push_back(1);
for(int j = 0; j < ans[i-2].size()-1; j ++) {
layer.push_back(ans[i-2][j] + ans[i-2][j+1]);
}
layer.push_back(1);
ans.push_back(layer);
}
return ans;
}
};
【leetcode】118. Pascal's Triangle的更多相关文章
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
- 【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】#118. Pascal's Triangle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【LeetCode】119. Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- leetcode笔记:Pascal's Triangle
一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【Leetcode】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 ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
随机推荐
- 【原创】SSAS-引用维度与多数据源、多数据源视图引发分区错误
背景: 最近有个项目,有32家分公司,集团总部需要取这个32家分公司数据做分析,由于每个分公司的数据都比较庞大,所以最终方案是每个分公司一个DW,在cube搭建过程中将每个公司数据作为一个分区数据的来 ...
- VirtualBox安装部署的Ubuntu16.04的步骤
1.下载ubuntu16.04镜像 http://cn.ubuntu.com/download/ 以及虚拟机软件VirtualBox https://www.virtualbox.org/wiki/D ...
- HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)
题目链接 2016 Qingdao Online Problem I 题意 在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...
- 扫面线+线段树(hdu1542)
之前写过这个算法,时间长了就忘掉了,,现在不看书自己努力回想起来,对算法的理解,对线段树的理解感觉也更深了一点(可能心理作用,哈哈哈) 思路简单说一下吧 从做到右遍历每一条矩阵的边(左右边),看该边对 ...
- PowerDisginer中NAME与COMMENT转换脚本
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the current model ' get t ...
- 访问控制技术- 标准IP访问列表
1.设置pc IP 网关 192.168.1.1 192.168.1.254 192.168.1.2 192.168.1.254 192.168.3.1 192.168.3.254 2.设置交换机借 ...
- UBIFS
转:http://www.armadeus.com/wiki/index.php?title=UBIFS This is a preliminary page dealing with the ins ...
- delphi怎样编译LINUX程序
delphi编译LINUX程序 DELPHI XE 10.2(TOKYO)开始可以开发LINUX控制台程序. 1)上传PASERVER到LINUX,并且运行PASERVER. 2)开始编译,PROFI ...
- VUE -- Mac上解决Chrome浏览器跨域问题
最近做前端开发总是遇到一个很奇怪的现象,同一个AJAX请求,在Chrome里调试的时候就会提示跨域,但是在手机模拟器或者真机上调试的时候就不会,于是百度了一下,发现是Chrome的安全策略导致的,需要 ...
- [Android Traffic] 调整定时更新的频率(C2DM与退避算法)
转载自: http://blog.csdn.net/kesenhoo/article/details/7395253 Minimizing the Effect of Regular Updates[ ...