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 ...
随机推荐
- hashCode方法里为什么选择数字31作为生成hashCode值的乘数
前提: 偶然的机会看到了大神的一篇博客,介绍的是hashCode()方法里为什么要用31这个数字作为生成hashCode的乘数.hashCode我在比较自定义类时曾经用到过 - 由于java默认比较的 ...
- Codeforces Round #396 (Div. 2) A
While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common s ...
- ssrs 里 表头 分页后显示
1. 列组,箭头,高级 2.点击行组,静态 3. 设置静态行组 属性
- c 语言写的高级Oracle®数据库调优及监控工具
http://www.lab128.com.cn/lab128_why.html ###另外一款ORALCE Monitor tool freee https://www.myorasql.com/ ...
- 119 Pascal's Triangle II 帕斯卡三角形 II Pascal's Triangle II
给定一个索引 k,返回帕斯卡三角形(杨辉三角)的第 k 行.例如,给定 k = 3,则返回 [1, 3, 3, 1].注:你可以优化你的算法到 O(k) 的空间复杂度吗?详见:https://leet ...
- 使用Yeoman 创建 angular应用
一.安装 Yeoman npm install yo -g 如果提示当前nodejs版本和npm版本太低,先升级下再安装yeoman. 安装成功后,默认只有webapp和Mocha这两个生成器. 二. ...
- Linux下命令行中的复制和粘贴
安装gpm:yum install -y gpm* 开启gpm服务:systemctl start gpm 按住鼠标左键,选中想要复制的内容,松开就完成复制,再在复制的位置按右键就完成粘贴.
- Nodejs chrome 调试node-inspector
1.下载扩展: 全局安装 npm install -g node-inspector 2.开启debug调试: node --debug[=port] filename (默认端口5858)node ...
- arcgis jsapi接口入门系列(6):样式
symbol: function () { //线样式 //样式详情请看官方文档 let style = { //线颜色,支持多种格式: //CSS color string:例如"dodg ...
- Mybatis配置多数据源
一. Spring配置多数据源 二. Spring配置数据源 三. MultipleDataSource的实现 1: package com.wbl.modal; 2: 3: import org. ...