题目

Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5,

Return

分析

构建数字金字塔,由上图可以清楚的找到规律。

该题目可用递归实现!

比较简单~

AC代码

class Solution {
public:
vector<vector<int>> generate(int numRows) {
if (numRows == 0)
return vector<vector<int>>();
else if (numRows == 1)
return vector<vector<int>>(1, vector<int>(1, 1)); //存储当前金字塔
vector<vector<int> > ret = generate(numRows - 1);
//计算当前行
vector<int> cur(numRows, 0);
cur[0] = 1;
cur[numRows - 1] = 1; for (int i = 1; i < numRows - 1; ++i)
{
cur[i] = ret[numRows - 2][i - 1] + ret[numRows - 2][i];
}//for
ret.push_back(cur); return ret;
}
};

GitHub测试程序源码

LeetCode(118) Pascal's Triangle的更多相关文章

  1. LeetCode(119) Pascal's Triangle II

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

  2. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. LeetCode(118):杨辉三角

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

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. 在 Java 的多线程中,如何去判断给定的一个类是否是线程安全的(另外:synchronized 同步是否就一定能保证该类是线程安全的。)

    同步代码块和同步方法的区别:同步代码块可以传入任意对象,同步方法中 如果多个线程检查的都是一个新的对象,不同的同步锁对不同的线程不具有排他性,不能实现线程同步的效果,这时候线程同步就失效了. 两者的区 ...

  2. vuex2 10分钟快速入门

    因为太简单了,我直接就贴代码了~ #建立store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex) export de ...

  3. React 实践记录 03 React router

    Introduction 本文主要参考了react router 的官方文档. React Router是一套完整的配合React的路由解决方案,可能你已经知道前端路由,或者知道后端有路由的概念,如下 ...

  4. 安卓linux真机调试

    原文链接:https://www.zhihu.com/question/35517675 你使用的是Linux,请遵以下步骤执行. 以root用户执行adb kill-server 以root用户执行 ...

  5. 单线程异步回调机制的缺陷与node的解决方案

    一.node单线程异步的缺陷: 单线程异步的优点自然不必多说,node之所以能够如此快的兴起,其单线程异步回调机制相比于传统同步执行编程语言的优势便是原因之一.然而,开发一个node程序,其缺陷也是不 ...

  6. 兼容IE的滚动条自定义样式

    废话不多说,直接上: IE专属的滚动条样式定义,只能设置各种原始结构的颜色,宽高结构等其他样式无法修改: div{ scrollbar-arrow-color: red; /*三角箭头的颜色*/ sc ...

  7. iOS 自定义读写文件

    LSCacheFile.h // // LSCacheFile.h // iPhone // // Created by xujinzhong on 14-6-5. // Copyright (c) ...

  8. 查找算法(顺序查找、二分法查找、二叉树查找、hash查找)

    查找功能是数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数组中没有重复的数据存在. (1)顺 ...

  9. Azure Powershell 获取可用镜像 PublisherName,Offer,Skus,Version

    #登录 $username="{登录名}" #定义一个用户账号的变量,可以输入需要登录的订阅账号名称 $password=ConvertTo-SecureString -Strin ...

  10. -bash: mail: command not found

    近日,安装了一个最小化的centos 6.3 6,用mail发送邮件进行测试的时候提示-bash: mail: command not found mailx没有安装,于是: yum -y insta ...