【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, 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的更多相关文章
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【leetcode】118. Pascal's Triangle
@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...
- 【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 ...
- 【easy】118.119.杨辉三角
这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【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 ...
- 【LeetCode】462. 最少移动次数使数组元素相等 II
给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2,3] 输出: 2 说明: 只 ...
随机推荐
- Oracle ->> Oracle下生成序列的方法
用hierachical query,即connect by配合dual表生成序列,mod这个是取余函数,生成group factor.最后面的connect by rownum <= 100可 ...
- Go语言学习笔记一(语法篇)
国庆节七天假期,这段时间刚好项目那边催的不是很紧,基本上每天都是白天重构一下项目代码,晚上自己学习.(大概是因为容总那边的人都去度假了把项目进度放慢了吧.这两天“彩虹”姐姐也来凑热闹,据说还是直接从澳 ...
- 《OD学hadoop》mac下使用VMware Fusion安装centos
一. NAT模式网络访问 (1)在linux中输入命令ifconfig查看网络信息 (2)在mac中输入命令ifconfig查看网络信息 lo0: flags=<UP,LOOPBACK,RUNN ...
- JAVA设计模式之【简单工厂模式】
1.创建抽象类User public abstract class User // 抽象类 { public void sameOperation() { System.out.println(&qu ...
- 使用.9.png报错 Exception raised during rendering
Exception raised during rendering: Index: 2, Size: 2Exception details are logged in Window > Show ...
- JOIN_TAB
typedef struct st_join_table { st_join_table() {} /* Remove gcc warning */ TABLE *table; KEYUSE *key ...
- jQuery 停止动画、jQuery Callback 函数、jQuery - Chaining
一.jQuery 停止动画 jQuery stop() 方法用于在动画或效果完成前对它们进行停止. stop() 方法适用于所有 jQuery 效果函数,包括滑动.淡入淡出和自定义动画. $(sele ...
- linux CPU loading calculate
http://hi.baidu.com/lionpanther/item/e908c37abdaf1c3f71442380 #include <stdio.h>#include <s ...
- [反汇编练习] 160个CrackMe之022
[反汇编练习] 160个CrackMe之022. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- hihoCoder #1181: 欧拉路·二 (输出路径)
题意: 给定一个图,要求打印出任一条欧拉路径(保证图肯定有欧拉路). 思路: 深搜的过程中删除遍历过的边,并在回溯时打印出来.在深搜时会形成多个环路,每个环都有一个或多个结点与其他环相扣,这样就可以产 ...