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]
] 产生杨辉三角,比较简单,注意几个边界条件:
 class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<int> vi;
vector<vector<int> > ans;
int i,j;
vi.push_back();
ans.clear(); // 注意初始化
9 if(numRows<=0) return ans;
10 if(1==numRows)
11 {
12 ans.push_back(vi);
13 return ans;
14 }
15 if(2==numRows)
16 {
17 ans.push_back(vi);
18 vi.push_back(1);
19 ans.push_back(vi);
20 return ans;
21 } ans.push_back(vi);
vi.push_back();
ans.push_back(vi);
for(i=;i<numRows;i++)
{
vi.clear();
vi.push_back();
for(j=;j<i;j++)
{
vi.push_back(ans[i-][j-]+ans[i-][j]);
}
vi.push_back();
ans.push_back(vi);
}
return ans;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Pascal's Triangle的更多相关文章

  1. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  2. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

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

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  6. leetcode 【 Pascal's Triangle II 】python 实现

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...

  7. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  8. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  9. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

随机推荐

  1. centos7.3安装python3.6.5

    最近在玩django,想部署个网站试试,结果发现线上默认的centos用得居然是python2.7.5,那么先升级下吧,到python3.6.5 yum安装时python2.7.5 那么编译安装吧 那 ...

  2. 推荐一篇mysql优化干货

    淘宝的技术一直比较前沿,特别是LVS的作者加入淘宝后,淘宝和阿里的开源做的有声有色,君不见淘宝出了tengine,tairs,tddl,hsf(soa框架,未开源),tfs(小文件存储系统)等等,阿里 ...

  3. GNU/Linux操作系统总览

    计算机科学本科的专业课包括高等数学.离散数学.模拟电子技术.数字电子技术.微机原理.汇编语言原理.高级程序语言.操作系统原理.高级编译原理.嵌入式原理.网络原理.计算机组成与结构等诸多科目.GNU计算 ...

  4. Apache加载curl_init 失败 php_curl.dll

    Call to undefined function curl_init php.ini curl_init extension=php_curl.dll xampp\php\php.ini 2018 ...

  5. 佛祖保佑、永无BUG!!!

    /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...

  6. awk中printf的使用说明

    printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息.在编写程序时经常会用到此函数.printf()函数的调用格式为: printf("", ); 其中 ...

  7. svn 的限制

    *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo *.rej *~ #*# .#* .*.swp .DS_Store *.exe *. ...

  8. 封装baseservice

    package com.huawei.base; import java.io.Serializable;import java.util.List; public abstract class Ba ...

  9. 用django框架开发一个B2C购物网站的基本流程和用到的知识点总结1

    开发流程 开发模式采用前后端分离模式,作为后端开发人员我们只关注后端业务逻辑开发: 省略项目框架搭建文件的配置部分.... 一:用户部分 在项目开发中我们要用到用户模型类User,Django认证系统 ...

  10. Android中Activity的四种启动方式

    谈到Activity的启动方式必须要说的是数据结构中的栈.栈是一种只能从一端进入存储数据的线性表,它以先进后出的原则存储数据,先进入的数据压入栈底,后进入的数据在栈顶.需要读取数据的时候就需要从顶部开 ...