一天一道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. Spring--AOP 例子

    先用代码讲一下什么是传统的AOP(面向切面编程)编程 需求:实现一个简单的计算器,在每一步的运算前添加日志.最传统的方式如下: Calculator.Java package cn.limbo.spr ...

  2. HashSet<T>的妙用

    HashSet<int> hs = new HashSet<int>(); var ret = hs.Add(1);  //ret==true var ret2 = hs.Ad ...

  3. c++ Lambda表达式待修改

    C++11引入了lambda表达式,使得程序员可以定义匿名函数,该函数是一次性执行的,既方便了编程,又能防止别人的访问. Lambda表达式的语法通过下图来介绍: 这里假设我们定义了一个如上图的lam ...

  4. Mysql 统一设置utf8字符

    无聊的关于有效配置文件路径的备忘 原来阿里云服务器的mysql 5.5 , 配置/etc/my.cnf是没有任何作用的,需要编辑/etc/mysql/my.cnf 妈的, 就是这一点让我测试了两天, ...

  5. JBOSS EAP实战(1)

    JBOSS的诞生 1998年,在硅谷SUN公司的SAP实验室,一个年轻人正坐在电脑前面思考,然后写着什么东西.不,他没有在写程序,他在写辞呈.他正在做出人生的一个重大决定:他要辞掉在SUN的这份工作, ...

  6. VirtualBox: How to config higher screen resolution

    Issue: Default Screen Resolution in Virtualbox instance is 800*600 which might be too small for gene ...

  7. IT男的别样人生,爱折腾,竟然辞职跑丽江去了

    深圳待了4年,在深圳腾讯总部任职,北漂了5年多,任某知名团购公司CTO,有了孩子以后才知道自己想要什么 2015年4月,我和老婆还有6个月的儿子丽江游, 却在旅行的第四天, 买下了位于束河古镇正门的高 ...

  8. Dynamics CRM 产品视图列上自带按钮的隐藏

    CRM中对command bar的处理都是使用ribbon workbench,但是很多系统自带的按钮你是没法在ribbon workbench看到的,咱们以产品为例,比如我要隐藏form上的保存按钮 ...

  9. 解决linux删除文件后空间没有释放问题

    linux删除文件后沒有释放空间 今天发现一台服务器的home空间满了,于是要清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [ro ...

  10. 深度学习&机器学习资源汇总1

    本篇博客的目地,是对工作学习过程中所遇所见的一些有关深度学习.机器学习的优质资源,作分类汇总,方便自己查阅,也方便他人学习借用. 主要会涉及一些优质的理论书籍和论文.一些实惠好用的工具库和开源库.一些 ...