118 - Pascal's Triangle

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]
]

Solution:

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ret;
if(numRows==)return ret;
vector<int> vec(,);
ret.push_back(vec);
int i=;
while(i<numRows){
vec.clear();
vec.push_back();
for(int j=;j<ret[i-].size();j++){
vec.push_back(ret[i-][j-]+ret[i-][j]);
}
vec.push_back();
ret.push_back(vec);
i++;
}
return ret;
}
};

119 - 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,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

Solution:

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> vec(,);
vector<vector<int>> ret;
ret.push_back(vec);
int i=;
while(i<=rowIndex){
vec.clear();
vec.push_back();
for(int j=;j<ret[i-].size();j++){
vec.push_back(ret[i-][j-]+ret[i-][j]);
}
vec.push_back();
ret.push_back(vec);
i++;
}
return vec; //or return ret[rowIndex];
}
};

【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II的更多相关文章

  1. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

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

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

  4. 【LeetCode】118. Pascal's Triangle

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

  5. 【easy】118.119.杨辉三角

    这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...

  6. 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. 【LeetCode】122. Best Time to Buy and Sell Stock II

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  9. 【LeetCode】462. 最少移动次数使数组元素相等 II

    给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2,3] 输出: 2 说明: 只 ...

随机推荐

  1. 当滚动条滚动到页面底部自动加载增加内容的js代码

    这篇文章主要介绍了如何使用javscript实现滚动条滚动到页面底部自动加载增加页面内容,需要的朋友可以参考下..1,注册页面滚动事件,window.onscroll = function(){ }; ...

  2. (七)后台.apsx.cs获取前台客户端文本框的内容

    <input ID='AllLocalData' name='AllLocalDataName' /> 其中最重要的一点是Request.Form[]中括号是放的name属性而非Id属性. ...

  3. ccnu-线段树-单点更新3-C

    C - 单点更新3 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

  4. HDU 1054:Strategic Game

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. C#图片压缩的实现方法

    一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的.尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如果是需要高质量的图片也得需要生产缩略图. 一般在web应用中 ...

  6. SQL[连载1]简介

    SQL[连载1]简介 SQL 教程 SQL 是用于访问和处理数据库的标准的计算机语言. 在本教程中,您将学到如何使用 SQL 访问和处理数据系统中的数据,这类数据库包括:MySQL.SQL Serve ...

  7. visual studio 中删除多余的空白行

    替换  Ctrl+H  正则  勾选  替换 ^\s*\n 为空

  8. POJ3415 Common Substrings

    后缀数组 求长度不小于k的公共子串的个数 代码: #include <stdio.h> #include <string.h> ; int len, len1; int wa[ ...

  9. Flume学习 & Kafka & Storm 等 & Log4J 配置

    正在学习这篇文章: http://blog.csdn.net/ymh198816/article/details/51998085 和工作中接触的电商.订单.分析,可以结合起来. 开宗明义,这幅图片: ...

  10. 在HibernateTemplate里执行Sql语句

    如下所示只能执行非Select语句: public static void executeSQL(HibernateTemplate hibernateTemplate, String sql) { ...