题目:

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]
]

分析:打印杨辉三角。numRows=0 或者 numRows=1时特殊处理。

numRows>=2时都是有规律的了。

代码例如以下:

#include <iostream>
#include <vector>using namespace std;vector<vector<int> > generate(int numRows)
{vector<vector<int> > res;vector<int> currentRow;vector<int> lastRow;if(numRows <= 0)return res;currentRow.push_back(1);res.push_back(currentRow);if (numRows == 1)return res;for (int i=0; i<numRows-1; i++){lastRow = currentRow;currentRow.clear();currentRow.push_back(1);for(int j=0; j<lastRow.size()-1; j++)currentRow.push_back(lastRow[j] + lastRow[j+1]);currentRow.push_back(1);res.push_back(currentRow);}return res;
}int _tmain(int argc, _TCHAR* argv[])
{vector<vector<int> > res = generate(5);// 打印结果for (int i=0; i<res.size(); i++){vector<int> row = res[i];for (int j=0; j<row.size(); j++)cout << row[j] <<" ";cout << endl;}return 0;
}

转载于:https://www.cnblogs.com/xfgnongmin/p/10672271.html

Leetcode-Pascal's Triangle相关推荐

  1. LeetCode Pascal's Triangle II

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

  2. LeetCode Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  3. Leetcode: Pascal's Triangle II

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

  4. LeetCode——Pascal#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  5. LeetCode - Easy - 119. Pascal‘s Triangle II

    Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex ...

  6. LeetCode - Easy - 118. Pascal‘s Triangle

    Topic Array Description https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer ...

  7. [LeetCode]119.Pascal's Triangle II

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 Given an ind ...

  8. leetcode python3 简单题119. Pascal's Triangle II

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十九题 (1)题目 英文: Given a non-negative ind ...

  9. leetcode python3 简单题118. Pascal's Triangle

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十八题 (1)题目 英文: Given a non-negative int ...

  10. LeetCode 118 Pascal's Triangle(帕斯卡三角形)(vector)

    翻译 给定一个行数字,生成它的帕斯卡三角形.例如,给定numRows = 5, 返回: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1] ] 原文 Given numR ...

最新文章

  1. 20154318_王秀飞 Exp2 后门原理与实践
  2. Jinja2模板引擎简介
  3. windows系统命令行下常用命令收集
  4. background-attachment: fixed的用法
  5. 软件常用版本英文snapshot和ga
  6. mysql 核对_核对数据库表记录的shell脚本
  7. python 如何快速判断列表是否相同_Python-检查列表中的所有元素是否相同
  8. php getdbused,PHP之购物车
  9. matlab fft函数说明_关于“如何使用FFT计算天线阵列方向图”学习资料总结
  10. java并发编程之线程的生命周期详解
  11. oracle rac 创建ocr,转:Oracle RAC 添加和删除OCR(10g)
  12. [R语言绘图]plot函数的使用
  13. 基本知识 100159
  14. 【考题·习题详解】数论知识+数学推导
  15. mysql(多级分销)无限极数据库设计方法
  16. mysql空洞,mysql crud造成数据空洞的原因
  17. 什么是大数据?大数据又有什么用处?
  18. 抖音广告多少种,这些你知道吗?
  19. 树莓派——安装OpenCV
  20. Unity学习笔记:个人学习项目《疯狂天才埃德加》纠错文档

热门文章

  1. 安装mysql ubantu_安装mysql到ubuntu
  2. mac可以写linux的进程,macOS系统上读写Linux的ext4分区方法
  3. 集群服务器分析系统,集群服务器系统可扩展性的研究与实现
  4. Linux的/var/www/html目录
  5. 010. 深入JVM学习—垃圾收集策略概览
  6. redis的安装和常用命令
  7. 系统批量运维管理器Fabric详解
  8. O365(世纪互联)SharePoint 之使用Designer报错
  9. 第十五讲 循环体for基础
  10. 微软应用商店正式向中国开发者开放