LeetCode(118) Pascal's Triangle
题目
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;
    }
};
LeetCode(118) Pascal's Triangle的更多相关文章
- 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 [ ...
 - 【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, ...
 - LeetCode(118):杨辉三角
		
Easy! 题目描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1] ...
 - LeetCode(275)H-Index II
		
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
 - LeetCode(220) Contains Duplicate III
		
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
 - LeetCode(154) Find Minimum in Rotated Sorted Array II
		
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
 - 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 ...
 - LeetCode(116) Populating Next Right Pointers in Each Node
		
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
 - 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 ...
 
随机推荐
- python之 __getattr__、__getattr__、__getitem__、__setitem__ 使用
			
python之 __getattr__.__getattr__.__getitem__.__setitem__ 使用 __getattr__内置使用点号获取实例属性属性如 s.name,自调用__ge ...
 - appium环境搭建思路
			
1.appium环境是不是需要appium的一个安装包? 2.我们针对android进行测试我们是不是需要android本身的一个android 的sdk? 3.android这个本身就是java基础 ...
 - JS中的关系操作符与自动转型
			
很多时候对数据操做时都会遇到数据转换,有的是显示转化,有的是隐式转化,即调用默认的规则进行数据转换,经常会把数据转换的方式搞混,于是就花了点时间做了个小小的总结: 一元操作符(--,++,-,+)作用 ...
 - Unity Shader入门精要学习笔记 - 第14章非真实感渲染
			
转载自 冯乐乐的 <Unity Shader 入门精要> 尽管游戏渲染一般都是以照相写实主义作为主要目标,但也有许多游戏使用了非真实感渲染(NPR)的方法来渲染游戏画面.非真实感渲染的一个 ...
 - ecshop如何增加多个产品详细描述的编辑器
			
在做商产品详情的时候,经常会有选项卡类似的几个产品说明,如:商品详情,商品规格,参数列表,售后服务等. Ecshop后台里面默认只有一个编辑框(器),那么我们还得自己添加几个,以下是ecshop如何增 ...
 - 接口文档管理工具rap
			
git地址: https://github.com/thx/RAP wiki : https://github.com/thx/RAP/wiki/home_cn 视频教程: http://thx.g ...
 - 【algorithm】二叉树的遍历
			
二叉树的遍历 二叉树用例 代码解析: public class BinaryTree { static class TreeNode { Integer val; TreeNode left; Tre ...
 - 用Excel生成Sql
			
用Excel生成Sql: 以如图为例:点击一行数据的后面一个单元格,在上面的fx部分输入=,以等号开头证明这是一个公式.在等号的后面写上想要添加的数据,书写规范是这样:'"&A2&a ...
 - hihocoder1068 RMQ-ST算法
			
思路: 这是ST表模板.遇到一道indeed笔试题需要用这个算法,顺便学习一下.那道题是说给定一个一维数组和一些查询[Li, Ri],要求计算[Li, Ri]区间内子段和的绝对值的最大值.解法是使用S ...
 - webpack入门之最简单的例子 webpack4
			
webpack在目前来说应该是前端用的比较多的打包工具了,那么对于之前没有接触过这块的该怎么办呢?答案很明显嘛,看资料,查文档,自己去琢磨,自己去敲一敲,跑一跑: 那么,这边我将以一个最基础的例子来将 ...