一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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]

]

(二)解题

题目大意:求解杨辉三角

即每一行第i个数(除首尾元素等于1外),其他都等于上一行的第i-1个数和第i个数相加。

详细解释见代码注释:

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> ret;
        int n = 1;//从第1行开始
        while(n<=numRows)
        {
            vector<int> temp;
            for(int i = 0 ; i < n ; i++)
            {
                if(i==0||i==n-1) temp.push_back(1);//首尾等于1
                else{
                    temp.push_back(ret[n-2][i-1]+ret[n-2][i]);//其他的等于上一行的第i-1个加上第i个
                }
            }
            ret.push_back(temp);
            n++;
        }
        return ret;
    }
};

【一天一道LeetCode】#118. Pascal's Triangle的更多相关文章

  1. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  2. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  3. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  4. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  6. leetcode 118 Pascal's Triangle ----- java

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

  7. Java [Leetcode 118]Pascal's Triangle

    题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  8. Java for LeetCode 118 Pascal's Triangle

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

  9. Leetcode 118 Pascal's Triangle 数论递推

    杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...

  10. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

随机推荐

  1. TRIM ,LTRIM ,RTRIM ,空格过滤

  2. 利用Apach ab对nodejs进行并发负载的压力测试

    大家应该都有听过,nodejs性能优越,并发也很好之类的话,那我们用Apache ab对node这个空框架测试一下,然后再对一些网站测试一下,或或少一定的参考意义把. Apache ab测试工具是模拟 ...

  3. springboot解决跨域问题(Cors)

    1.对于前后端分离的项目来说,如果前端项目与后端项目部署在两个不同的域下,那么势必会引起跨域问题的出现. 针对跨域问题,我们可能第一个想到的解决方案就是jsonp,并且以前处理跨域问题我基本也是这么处 ...

  4. AsyncLocal 与 async await

    大家来看一张图 先猜猜看为什么会这样 关于async await的原理 建议查看 https://blog.csdn.net/brook_shi/article/details/50803957 这篇 ...

  5. hive 存储,解析,处理json数据

    hive 处理json数据总体来说有两个方向的路走 1.将json以字符串的方式整个入Hive表,然后通过使用UDF函数解析已经导入到hive中的数据,比如使用LATERAL VIEW json_tu ...

  6. 解析配置文件redis.conf

    units单位: # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 ...

  7. python if判断语句&计算

    python对缩进要求严格,代码块里的缩进必须一样,可以常用 tab键  表示4个空格 if 条件: 代码块 else: if判断语句如下: 1 print("吃饭,喝水,回家") ...

  8. 网络安全之在Kali Linux上安装Openvas

    本文目录: 一.解决和配置更新源问题 二.安装Openvas 三.自定义登陆密码 四.升级Openvas 五.查看Openvas运行情况 六.修改OpenVAS远程链接 =============== ...

  9. Appium--入门demo

    Appium环境搭建已经在在博客中写出 http://www.cnblogs.com/feimaoyuzhubaobao/p/5057832.html   那么本篇博客主要介绍java版本的appiu ...

  10. webpack 1.x 配合npm scripts管理多站点

    需求场景: 希望通过一个webpack文件夹管理多个站点的打包流程. 假设现在我要为站点domain配置打包流程. npm 添加淘宝镜像 你懂得 vim ~/.npmrc registry = htt ...