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. C指针笔记

    指针的学习 两个数比较大小,通过传递内容进行比较 #include <stdio.h> void swap(int *p1, int *p2){ int temp; //注意指变量*的两个 ...

  2. 一个月时间整理《深入浅出Node.js》

    今天终于把朴灵老师写的<深入浅出Node.js>给学习完了, 这本书不是一本简单的Node入门书籍,它没有停留在Node介绍或者框架.库的使用层面上,而是从不同的视角来揭示Node自己内在 ...

  3. WPF学习笔记3——Layout之1

    一.概述 了解XAML的基本之后,进入Layout的学习.Layout,即布局,可能需要用到几种不同的容器.每一种容器都有各自的逻辑.在用户界面的设计过程中,很多时候是在想办法使得界面更加吸引.实在. ...

  4. Network boot from AMD Am79C970A

    用虚拟机安装winxp系统,出现错误信息如下: Network boot from AMD Am79C970A Copright (c)2003-2005 Vmware, Inc. Copright ...

  5. struts2用了哪几种模式

    代理模式 责任连模式 ActionVacation 迭代模式

  6. 如何解决jenkins中shell脚本明明执行失败却不自行退出,且构建结果仍然显示success的问题??

    首先,需要明确shell命令执行结果$?为0或者非0仅能代表此执行语句是否顺利执行了,例如: 执行语句:adb connect 192.168.XX.XX 执行结果:unable to connect ...

  7. DOS下文件操作命令

    文件名是由文件路径和文件名称合起来的,如C:\DOS\COMMAND.COM. DIR 显示文件和文件夹(目录). 用法:DIR [文件名] [选项] 它有很多选项,如/A表示显示所有文件(即包括带隐 ...

  8. WebService积累

    1.缺点,无法传输复杂对象:如无法序列化key/value结构的类型参数以及相关一维集合接口(Hashtable等打上标签[Serializable]即可序列化,不过继承的IDictionary并不可 ...

  9. [JavaScript] 用html5 js实现浏览器全屏

    项目中需要将后台浏览器的窗口全屏,也就是我们点击一个按钮要实现按F11全屏的 效果. 在HTML5中,W3C制定了关于全屏的API,就可以实现全屏幕的效果,也可以 让页面中的图片,视频等全屏目前只有g ...

  10. 卡牌手游源码《暗黑世界V1.3》数据库表说明文档!!!

    原地址:http://blog.csdn.net/uxqclm/article/details/11970761 欢迎来到9秒:www.9miao.com 由于看到论坛中有人询问需求<暗黑世界V ...