@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&#39;s Triangle的更多相关文章

  1. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  2. 【LeetCode】118. Pascal's Triangle

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...

  3. 【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, ...

  4. 【一天一道LeetCode】#118. Pascal's Triangle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  5. 【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 [ ...

  6. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  7. 【一天一道LeetCode】#119. Pascal's Triangle II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

随机推荐

  1. virtualbox centos6.6 minimal 与宿主机win7共享文件夹

    1.virtualbox, 设置-共享文件夹,设置好共享文件夹win7dir 2.centos 创建文件夹比如share 3.mount -t vboxsf win7dir /root/share 4 ...

  2. 手机估值计算的jquery代码

    <script type="text/javascript"> $('#inquiry').click(function(){ var total=0; var cou ...

  3. Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)

    题目链接  Round #458 (Div. 1 + Div. 2, combined)  Problem D 题意  给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...

  4. #423 Div2 C

    #423 Div2 C 题意 给出 n 个字符串以及他们在 S 串中出现的位置,求字典序最小的 S 串.保证给出的字符串不会冲突. 分析 模拟就好.用并查集思想优化,数组 nxt[i] 表示从 i 开 ...

  5. android利用adb shell查看activity的栈

    Android中怎么查看应用的activity栈? 1. 进入adb shell 2.可以直接输入dumpsys ,可以查看device的一些信息如 3.也可以直接输入 dumpsys activit ...

  6. Spoj SUBLEX - Lexicographical Substring Search

    Dicription Little Daniel loves to play with strings! He always finds different ways to have fun with ...

  7. [TopCoder8600]MagicFingerprint

    题目大意: 定义magic(x)为将x按十进制顺序写下来,依次对相邻两个数写下差的绝对值,并去除前导0得到的新数. 若对得到的magic(x)重复进行多次magic,最后会变成一个一位数. 若最后变成 ...

  8. EF for oracle中无法读取配置 显示无法open问题解决方式

    1.更新以上设置为 非注销部分 弄了很久很久哈.

  9. Java 根据年月日精确计算年龄

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  10. Features (OCMock 2)

    This page describes the features present in OCMock 2.x, using the traditional syntax. All these feat ...