Pascal's Triangle

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]
]

c++代码如下:

#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        int i;
        vector<vector<int> > vec(numRows);
        for(i=0;i<numRows;i++)
           vec[i]=getRow(i);

        return vec;
    }
    vector<int> getRow(int rowIndex) {
        if(rowIndex==0)
            return {1};
        vector<int> vec1(rowIndex);
        vector<int> vec2(rowIndex+1);
        vec1=getRow(rowIndex-1);
        vec2[0]=1;
        for(int i=1;i<rowIndex;i++)
        {
            vec2[i]=vec1[i-1]+vec1[i];
        }
        vec2[rowIndex]=1;
        return vec2;
    }
};

int main()
{
    Solution s;
    int n;
    cout<<"input n is :";
    cin>>n;
    vector<vector<int> > vec=s.generate(n);
    for(auto v:vec)
    {
        for(auto a:v)
            cout<<a<<" ";
        cout<<endl;
    }
    cout<<endl;
}

运行结果:


 

时间: 2024-07-31 15:35:34

Pascal&#39;s Triangle的相关文章

[LeetCode]118.Pascal&amp;#39;s Triangle

题目 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] ] 思路 模拟 代码 /**------------------------------------ * 日期:2015-02-06 * 作者:SJF0115 * 题目: 118.Pascal's

[LeetCode]119.Pascal&amp;#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,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路 无 代码 /**------------------------------------ * 日期:2015-02-06 * 作者:S

【LeetCode从零单排】No118 Pascal&amp;#39;s Triangle

题目 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] ] 代码 public class Solution { public List<List<Integer>> generate(int numRows) { List<List

Pascal&amp;#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,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 用递归的方法实现,C++代码如下: #include<iostream> #include<vector> using namespac

基于Java实现杨辉三角 LeetCode Pascal&#039;s Triangle_java

 Pascal's Triangle 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] ] 这道题比较简单, 杨辉三角, 可以用这一列的元素等于它头顶两元素的和来求. 数学扎实的人会看出, 其实每一列都是数学里的排列组合, 第4行, 可以用 C30 =

php 解决MySQL插入数据出现 Incorrect string value: &amp;amp;#39;\xF0\x9F\x92\x8BTi...&amp;amp;#39;错误

在项目中向MySQL插入数据时,发现数据插入不完整,通过调试,发现插入语句也没什么特殊的错误.但是就是差不进去,于是就打开mysqli错误的调试 $ret = mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn)); 结果弹出如下错误信息: Incorrect string value: '\xF0\x9F\x92\x8BTi...' 有错误信息就好办了,结果上网一查结果是:mysql编码格式utf-8格式,不支持带四

apache2.0.39 php4.2.3在windowsXP下模块方式搭建.

apache|window WindowsXP+Apache2.0.39+php-4.2.3-dev源文件下载: 1. http://www.apache.org/dist/httpd/binaries/win32/ 下面的 apache_2.0.39-win32-x86-no_ssl.msi 或者apache_2.0.39-win32-x86-no_ssl.exe (A full setup package (.exe) containing the Win9x/WinNT Microsoft

Dreamweaver MX 2004视频宝典教程(39)

dreamweaver|教程 第 39 集:插入图片 课程目标:学会如何在Dreamweaver中插入图像并设置属性的方法. 课程要点:Dreamweaver中插入图片的方法.<img>标记属性常见的有src, width, height, lowsrc, alt, border, vspace, hspace, align等,这些属性可以在Dreamweaver属性面板中进行可视化设置. [全屏观看] | [下载视频] | 本教程尺寸为 800 * 600 建议下载观看,以达到最佳观看效果

SEO总结:百度K站39天后放出且有排名

年前一站,被百度在1月12时K掉了,site首页都找不到了,之至后还以为会等很久,才会放出来呢,没有想到的是,在本月20号就放出来了. 查看了mytool上的快照记录,前后总共也就是39天的时候,百度就把它给放出来了.可能很多朋友,感觉都不大可能,百度怎么会这么快就放出一个K掉的站呢?下面说说在这网站被百度K掉这段时间里,我所做的事. 1.在查询网站被百度K掉后,当时我就把百度蜘蛛给禁止了,但是禁止的时间不长,只禁止一个星期多点这样子.禁止太久容易让百度产生,这站可能是永久性不要百度抓取的.反而