【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 说明: 只 ...
随机推荐
- 编辑器Emacs下载网址(中国镜像)
Root gnu emacs windows File Name ↓ File Size ↓ Date ↓ Parent directory/ - - README 14K 2014-Nov-15 ...
- Jquery正则表达式公式.例子
1.非负整数 /^\d+$/ 2.正整数 /^[0-9]*[1-9][0-9]*$/ 3.非正整数 /^((-\d+)|(0+))$/ ...
- Eclipse无法通过FileExplore打开真机data目录
ok ——> Eclipse无法通过FileExplore打开真机data目录 ref:http://blog.csdn.net/koyoter/article/details/7657440 ...
- USACO Section 2.3: Money Systems
简单的dp题 /* ID: yingzho1 LANG: C++ TASK: money */ #include <iostream> #include <fstream> # ...
- ajax练习习题一弹窗查看
显示页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- android下使用smack需引入的包
compile 'org.igniterealtime.smack:smack-android:4.2.0-alpha1' compile 'org.igniterealtime.smack:smac ...
- Eclipse下运行Maven项目提示缺少maven-resources-plugin:2.4.3
将一个手动创建的Maven项目(命令行下可正常运行)导入到Eclipse中,运行时提示这样的错误信息:[ERROR] Plugin org.apache.maven.plugins:maven-res ...
- msmq中消息的数量
using System.Diagnostics; PerformanceCounter objCounter = new PerformanceCounter("MSMQ Queue&qu ...
- Mac 切换Windows 使用虚拟机, 不推荐双系统
为什么使用虚拟机而不是双系统? 1.虚拟机可以随时在两个系统之间进行切换,便于在工作时使用而不影响效率.如果是双系统,在切换到另一个系统时需要关机重启,太过麻烦. 2.虚拟机除了运行Windows之 ...
- DataGridView中的单元格提示错误信息
http://stackoverflow.com/questions/7713988/winforms-problems-validating-a-cell-in-a-datagridview