【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 ...
随机推荐
- 利用System.Net.Mail 发送邮件
我这里只是试了一下发mail的功能,感觉.net自带的发mail是比较全的,还是直接上我的code 参数文章:System.Net.Mail 发送邮件 SMTP协议 using System; usi ...
- 微信小程序 使用swiper制作一个滑动导航
最近在做一个导航的时候,发现使用overflow-x: auto来做多内容滑动导航效果很不好,思索是不是可以使用swiper来做一个,研究了下其实发现原理基本相同 这里说下,要用swiper做导航菜单 ...
- Educational Codeforces Round 33 (Rated for Div. 2) A. Chess For Three【模拟/逻辑推理】
A. Chess For Three time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- SPOJ 3267 DQUERY - D-query (主席树)(区间数的种数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- ARM常用汇编指令介绍
b 跳转指令(跳转范围为32Mb) bl 带返回地址的跳转,指令自动将下一条指令的地址复制到R14寄存器,然后跳转到指定地址去执行,执行完后返回到下一条指令处执行 pc 寄存器R1 ...
- 把我的漫画浏览器后台程序迁移到GAE上了
这两天看了一下Python和GAE相关资料,作为练手,把我以前写的Windows 8下看漫画的程序的后台解析算法迁移到了GAE上了. 之前由于没有后台服务器,很多东西在本地实现起来不是很方便,现在拿G ...
- VUE -- 十分钟入门 Less
这篇文章来自 Danny Markov, 是我最喜欢的博主之一,实际上我最近翻译的一些文章全是出自他手.在查看本文之前你也可以 查看原文. 我们都知道写 CSS 代码是有些枯燥无味的,尤其是面对那些成 ...
- 完全理解Gson(3):Gson反序列化
完全理解Gson(2):Gson序列化 完全理解Gson(1):简单入门 本文延续前一篇文章,继续介绍简单基本的Gson用法.这篇文章我们将介绍如何将复杂的JSON对象解析为Java对象,其中Java ...
- Windows API常用函数
转自:http://www.cnblogs.com/xiashengwang/p/4026259.html .NET中虽然类库很强,但还是有些时候功能有限,掌握常用的api函数, 会给我们解决问题提供 ...
- 116 - Unidirectional TSP(DP)
多段图的最短路问题 . 运用了非常多的技巧 :如 记录字典序最小路径 . 细节參见代码: #include<bits/stdc++.h> using namespace std; con ...