一天一道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. Win2003及2008R2重启自动登录设置方法

    在windows系统中,使用最多的可能就是远程操作了,关于远程操作的那些事很多用户还是有些迷茫的.如果win2003系统远程重启后,要重新登录系统才能启用远程功能,这就十分的麻烦,如何才能实现重启后的 ...

  2. VMWare 学习目录

    Linux介绍 Linux入门--个人感想 Google怎么用linux 初入Linux Windows XP硬盘安装Ubuntu 12.04双系统图文详解 实例讲解虚拟机3种网络模式(桥接.nat. ...

  3. xadmin库的下载安装及奇葩报错的解决方法

    今天主要讲xadmin库的下载和安装的.......各种问题....... 先注明:我使用的是python3.6,Django2.0,所以xadmin也应该是2.0版本会比较适配. 所以这里先给个xa ...

  4. java 左移 右移

    public class test{ public static void main(String[] args) { int m = 9; int n = m >> 3; int p = ...

  5. 浏览器控制台调试json数据

    var str ='{"code":0,"message":"","systemTime":"2017-10- ...

  6. python文件转exe

    .py文件转exe文件 1.软件说明: 用python写一个视频处理软件,用到的库是moviepy 2.所用软件: Python 3.6.5 32位 pycharm  专门的python编辑ide,推 ...

  7. 216. Combination Sum III(medium, backtrack, 本类问题做的最快的一次)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. My Stuck in C++

    My Stuck in C++ Zhong-Liang Xiang Oct. 1st, 2017 这个专题记录了对于我而言, c++迷一样的东西.

  9. MongoDB 条件操作符

    描述 条件操作符用于比较两个表达式并从mongoDB集合中获取数据. 在本章节中,我们将讨论如何在MongoDB中使用条件操作符. MongoDB中条件操作符有: (>) 大于 - $gt (& ...

  10. 自定义一个View作为弹出对话框

    public void dialog(){ //获得布局对象 View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialo ...