leetcode—pascal triangle
1.题目描述
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] ] |
2.解法分析
这个题目很简单,所以不需要额外的解说,一遍就AC了
class Solution { public: vector<vector<int> > generate(int numRows) { // Start typing your C/C++ solution below // DO NOT write int main() function //by areslipan@163.com vector<vector<int> > pascal; if(numRows <= 0)return pascal; vector<int> firstRow ; firstRow.push_back(1); pascal.push_back(firstRow); if(numRows == 1)return pascal; firstRow.push_back(1); pascal.push_back(firstRow); if(numRows == 2)return pascal; for(int i = 2;i<numRows;++i) { vector<int> curRow; curRow.assign(i+1,0); curRow[0]= 1; curRow[i] =1; for(int j =1;j<i;++j) { curRow[j]=pascal[i-1][j-1]+pascal[i-1][j]; } pascal.push_back(curRow); } return pascal; } }; |
leetcode—pascal triangle的更多相关文章
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- [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 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- [leetcode]Pascal's Triangle @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- 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 ...
随机推荐
- mysql存储过程讲解
1.数据库存储过程:简单滴说,存储过程就是存储在数据库中的一个程序. 2..数据库存储过程作用: 第一:存储过程因为SQL语句已经预编绎过了,因此运行的速度比较快. 第二:存储过程可以接受参数.输出参 ...
- PL/SQL — 存储过程
存储过程子程序的一种类型,能够完成一些任务,作为schema对象存储于数据库.是一个有名字的PL/SQL代码块,支持接收或不接受参数,同时也支持参数输出.一个存储过程通常包含定于部分,执行部分,Exc ...
- 网络请求 post 的接受请求头的代理方法
接收响应头 content-Length content-Type - (void)connection:(NSURLConnection *)connection didReceiveRespons ...
- C# - 定义集合,索引符
Animal 类 Cow类 Chicken类 Animal 集合类 调用
- mvc3.0中[ValidateInput(false)]失效的问题
在asp.net mvc3.0中[ValidateInput(false)]特性失效了,只需要在网站根目录中的web.config中做如下配置即可: <system.web> <ht ...
- 关于 IIS 上的 Speech 设置
在之前的开发过程中,发现在 微软各个版本的Speech中(从sdk5.1 到 最新的 Speech PlatFarme V11),在本地可以生成音频文件,但是在IIS上却生成无法生成完整的文件. 调试 ...
- 用JavaScript探测页面上的广告是否被AdBlock屏蔽了的方法
每个人都讨厌广告.看电视.看电影.看优酷.看网页时,对满天飞的广告也是深恶痛绝.广告是一个不招人喜欢的东西.但是,对一个中小网站站长/博客主来说,广告几乎是唯一的能成支持网站/博客正常运转的资金来源. ...
- Java中的数组问题
java.util.Arrays This class deals with 'real' arrays in java, in the form of T[]. Thus it doesn't d ...
- Ubuntu下使用ap-hotspot出现“Another process is already running"问题的解决方案
参考Problem with ap-hotspot 问题描述: This is the message displayed in my terminal screen when I typed sud ...
- StreamCQL
StreamCQLhttps://github.com/HuaweiBigData/StreamCQL http://blog.csdn.net/viewcode/article/details/90 ...