zoukankan      html  css  js  c++  java
  • LeetCode 118. 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]
    ]
    class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> fresult;
            if (numRows == 0)
            {
                return fresult;
            }
            vector<int> result(numRows , 0);
            result[0] = 1;
            result[1] = 1;
            vector<int> temp(1, 1);
            fresult.insert(fresult.cend(), temp);
            if (numRows == 1)
            {
                return fresult;
            }
            vector<int> temp1(2, 1);
            fresult.insert(fresult.cend(), temp1);
            if (numRows == 2)
            {
                return fresult;
            }
            for (int i = 2;i <= numRows-1;++i)
            {
                vector<int> temp(result);
                result[i] = 1;
                for (int j = 1;j < i;++j)
                {
                    result[j] = temp[j] + temp[j - 1];
                }
                vector<int> temp1(result.begin(), result.begin() + i + 1);
                fresult.insert(fresult.end(), temp1);
            }
            return fresult;
        }
    };
  • 相关阅读:
    学习笔记
    django中嵌入百度editor插件
    定位屡试不爽
    django忘记管理员账号和密码处理
    linux上配置java环境
    python3学习问题汇总
    Android系统框架
    python深复制和浅复制
    装饰器原理和装饰器参数使用
    小白神器
  • 原文地址:https://www.cnblogs.com/csudanli/p/5749368.html
Copyright © 2011-2022 走看看