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. 工作流(Workflow)学习---基础知识整理

    工作流定义: 工作流是将一组任务组织起来以完成某个经营过程:定义了任务的触发顺序和触发条件,每个任务可以由一个或多个软件系统完成,也可以由一个或一组人完成,还可以由一个或多个人与软件系统协作完成. 工 ...

  2. 将TIBCO Host 实例注册为Windows服务

    安装了TIBCO ActiveMatrix BPM及成功创建了ActiveMatrix Administrator 和 BPM Server后,每次都要手动启动tibcohost,比较麻烦,实际上TI ...

  3. C和BlockCode

    在使用code block的时候,需要先build,然后再run,否则run的还是上次编译的内容.

  4. 渲染统计窗口(Rendering Statistics Window)

    FPS(Timer per frame and FPS) frames per seconds表示引擎处理和渲染一个游戏帧所花费的时间,该数字主要受到场景中渲染物体数量和GPU性能的影响,FPS数值越 ...

  5. Catch Application Exceptions in a Windows Forms Application

    You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. Thi ...

  6. UI表单

    Monk.UI表单美化插件诞生记!   阅读目录 背景 预览效果 表单组件 普通文本框 多行文本框 复选框 切换滑块 单选框 下拉选择框 数字输入框 时间选择 文件选择 显示文本 按钮 开源地址 背景 ...

  7. 我的PHP之旅--PHP的函数初步认识

    函数 函数主要是将一块代码封装起来方便多次使用,方便以后维护,节省代码. 先看一个简单的函数: <?php function myFirstFunc(){ echo "Hello PH ...

  8. 计算S(n)=a+aa+aaa+...... 其中a是一个数字

    描述 计算S(n)=a+aa+aaa+...... 其中a是一个数字 输入数据 两个分别表示a和n的整数 输出数据 一个表示S(n)的整数 输入示例 3 5 输出示例 37035 # include ...

  9. [转载]C#实现POST提交方式

    网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser.WebClient.HttpWebRequest这三 ...

  10. codeforces 390D Inna and Sweet Matrix

    几个小结论: 1.路径长度=i+j-1; 2.最简单的走法是先横走再竖着走或者先竖着走再横着走 #include<cstdio> #include<cstring> using ...