问题

给出变量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)的更多相关文章

  1. leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  2. leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  3. 118. 119. Pascal's Triangle -- 杨辉三角形

    118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  4. 【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, ...

  5. LeetCode(118) Pascal's Triangle

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

  6. 【一天一道LeetCode】#119. Pascal's Triangle II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  8. 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 ...

  9. 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, ...

随机推荐

  1. linux有用网址

    正则表达式在线测试 http://tool.oschina.net/regex

  2. WEB组件之间的关系

    WEB组件之间的关系: A:重定向的特点: 1:发生客户端 2:地址栏发生变化 3:两个WEB组件不共享request的数据. 4 重定向只能传递文本类型数据 服务端的方法:response.send ...

  3. PHP学习之[第03讲]PHP5.4 语法、常量、变量、数据类型详解

    1.<?php echo "Hello World"; ?> 2.//注释1,#注释2,/* 注释3 */ 3.系统常量: __FILE__默认常量,是指PHP程序文件 ...

  4. Ext.Panel的主要功能

    介绍面板组件的主要配置项及经常用法,这些配置项及方法将在后面的演示样例中用到,能够把这部分内容作为兴许章节的铺垫,进行高速的浏览,Ext.Panel主要配置项目如表5-1所看到的. 表5-1  Ext ...

  5. 更改Zend Studio/Eclipse代码风格主题

    最近决定把几个IDE的代码样式统一一下,Visual Studio的还算好改,PHP目前用得不多,不过也打算给Zend Studio换身新装. 网上搜索的一些更改Zend Studio主题的多是修改或 ...

  6. Java实现BASE64编解码

    Java实现BASE64编解码 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs BASE64和其它类似的编码算法通经常使用于转换二进制数据为文本数据,其目 ...

  7. Linux 下安装包制作

    一 Linux安装文件 Linux常见的安装为tar,zip,gz,rpm,deb,bin等.我们可以简单的分为三类, 第一:打包或压缩文件tar,zip,gz等,一般解压后即可,或者解压后运行sh文 ...

  8. Android四大组件——Service

    Service相关链接 Service初涉 Service进阶 Service精通 Service是Android系统中的一种组件,它跟Activity的级别差不多,但是它不能自己运行,只能后台运行, ...

  9. EntityFramwork6 在项目中的应用实例

    在项目开发中使用的ROM大多采用EntityFramwork去完成,下边给出最新的EntityFramwork在项目中的应用实例 : 一.更新EntityFramwork 在安装.NetFramwor ...

  10. 进程ps、kill 、grep

    linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有 ...