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的更多相关文章

  1. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  2. [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, ...

  3. [LeetCode] Pascal's Triangle 杨辉三角

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

  4. LeetCode——Pascal's Triangle

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

  5. LeetCode——Pascal&#39;s Triangle

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

  6. [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...

  7. [leetcode]Pascal's Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...

  8. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  9. LeetCode——Pascal&#39;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. log4j记录运行日志

    1.在工程中导入log4j-1.2.15.jar的jar包2.新建测试类 package control; import org.apache.log4j.Logger; import org.apa ...

  2. Java基础知识学习

    1.什么是Java编程语言 Java是:一种编程语言.一种开发环境.一种应用环境.一种部署环境 2.Java编程语言的主要目标 (1)提供一种解释环境为:提高开发速度.代码可移植性.使用户能运行不止一 ...

  3. codevs 1540 银河英雄传说

    题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米 ...

  4. GCC 警告提示的用法

    转自GCC 警告提示的用法 本节主要讲解GCC的警告提示功能.GCC包含完整的出错检查和警告提示功能,它们可以帮助Linux程序员写出更加专业和优美的代码.我们千万不能小瞧这些警告信息,在很多情况下, ...

  5. TSS 内核栈 用户栈的关系

    http://blog.sina.com.cn/s/blog_673ef8130100qaje.html 该博客不错,有不少有用的信息 中断程序的一开始我们执行一个PUSHALL,把这些积存器保存在核 ...

  6. Spring事务传播特性的浅析——事务方法嵌套调用的迷茫

    Spring事务传播机制回顾 Spring事务一个被讹传很广说法是:一个事务方法不应该调用另一个事务方法,否则将产生两个事务.结果造成开发人员在设计事务方法时束手束脚,生怕一不小心就踩到地雷. 其实这 ...

  7. jquery checkbox获取多个选项

    http://www.jb51.net/article/27186.htm http://www.cnblogs.com/libingql/archive/2011/11/07/2238663.htm ...

  8. 关于Python中的设计模式

    http://www.oschina.net/question/107361_25331 单例模式:Python 的单例模式最好不要借助类(在 Java 中借助类是因为 Java 所有代码都要写在类中 ...

  9. ruby 方法重载

    class MyClass def sayHello return "hello from MyClass" end def sayGoodbye return "Goo ...

  10. USACO3.23Spinning Wheels

    直接枚举角度 数据比较水吧 /* ID: shangca2 LANG: C++ TASK: spin */ #include <iostream> #include<cstdio&g ...