[LeetCode 118] - 杨辉三角形(Pascal's Triangle)
问题
给出变量numRows,生成杨辉三角形的前numRows行。
例如,给出numRows=5,返回:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
初始思路
基本算法和 杨辉三角形II(Pascal's Triangle II) 的基本一致。每算完一行的值将这些值拷贝一份到vector中即可。代码如下:
class Solution {
public:
std::vector<std::vector<int> > generate(int numRows)
{
std::vector<std::vector<int> > result;
std::vector<int> columnInfo;
if(numRows == )
{
return result;
}
columnInfo.push_back();
result.push_back(columnInfo);
if(numRows == )
{
return result;
}
columnInfo.push_back();
for(int i = ; i < numRows; ++i)
{
for(int j = i; j > ; --j)
{
//第一列和最后一列永远为1,不需要进行处理
if(j != && j != i)
{
columnInfo[j] = columnInfo[j - ] + columnInfo[j];
}
}
result.push_back(columnInfo);
//下一行开始列数相应增加,且最后一列的数字肯定是1
columnInfo.push_back();
}
return result;
}
};
generate
[LeetCode 118] - 杨辉三角形(Pascal's Triangle)的更多相关文章
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 118. 119. Pascal's Triangle -- 杨辉三角形
118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 【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, ...
- LeetCode(118) Pascal's Triangle
题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- Leetcode No.119 Pascal's Triangle II(c++实现)
1. 题目 1.1 英文题目 Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's tria ...
- LeetCode OJ 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, ...
随机推荐
- Flume源码-LoggerSink
package org.apache.flume.sink; import com.google.common.base.Strings; import org.apache.flume.Channe ...
- UVa 10025: The ? 1 ? 2 ? ... ? n = k problem
这道题仔细思考后就可以得到比较快捷的解法,只要求出满足n*(n+1)/2 >= |k| ,且n*(n+1)/2-k为偶数的n就可以了.注意n==0时需要特殊判断. 我的解题代码如下: #incl ...
- GDB错误:Cannot find bounds of current function
http://blog.csdn.net/zoomdy/article/details/17249165 mingdu.zheng <at> gmail <dot> com 使 ...
- 用java具体代码实现分数(即有理数)四则运算
用java具体代码实现分数(即有理数)四则运算 1,背景 Java老师布置了一个关于有理数运算的题目,因为参考书上有基本代码,所以自己主要是对书上代码做了一点优化,使其用户交互性更加友好以及代码封装性 ...
- Understanding AMQP, the protocol used by RabbitMQ--reference
RabbitMQ is a lightweight, reliable, scalable and portable message broker. But unlike many message b ...
- 超好用文件对比工具 – Beyond Compare
超好用文件对比工具 – Beyond Compare,开发中文件.目录对比神器,有了它,再也不用为找不到修改的内容而发愁了. 具备的丰富实用功能: 并列比较文件夹.FTP 网站或 Zip 文件: 为以 ...
- AS 自动生成选择器 SelectorChapek
简介 https://github.com/inmite/android-selector-chapek 设计师给我们提供好了各种资源,每个按钮都要写一个selector是不是很麻烦? 这么这个插件就 ...
- 在公网上布署Web Api的时候,不能调用,返回404
在internet上布署web API做的站点时,发现不能调用web api的任何action, 返回404. 经过很多的努力,也找不到原因,环境是win server 2008, IIS 75. n ...
- 黑马程序员-out和ref
C# 方法参数关键字:ref.out 当希望方法返回多个值时,声明 out方法很有用.使用 out参数的方法仍然可以将变量用作返回类型(请参见 return),但它还可以将一个或多个对象作为 out参 ...
- swing——JFrame基本操作
用JFrame(String String1)创建一个窗口 public void setBounds(int a,int b,int width,int height)设置窗口初始化的位置(a,b) ...