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

For example, given numRows = 5,

Return

  1. [
  2. [1],
  3. [1,1],
  4. [1,2,1],
  5. [1,3,3,1],
  6. [1,4,6,4,1]
  7. ]

  1. class Solution {
  2. public:
  3. std::vector<std::vector<int> > generate(int numRows) {
  4. std::vector<int> vec;
  5. std::vector<std::vector<int>> res(numRows,vec);
  6. int triangle[100][100];
  7. for (int i = 0; i < numRows; i++)
  8. {
  9. triangle[i][0] = 1;
  10. triangle[i][i] = 1;
  11. }
  12. for (int i = 2; i < numRows; i++)
  13. {
  14. for (int j = 1; j < i + 1; j++)
  15. {
  16. triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
  17. }
  18. }
  19. for (int i = 0; i < numRows; i++)
  20. {
  21. for (int j = 0; j <= i; j++)
  22. {
  23. res[i].push_back(triangle[i][j]);
  24. }
  25. }
  26. return res;
  27. }
  28. };

leetcode - Pascal&#39;s Triangle的更多相关文章

  1. LeetCode——Pascal&#39;s Triangle

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

  2. LeetCode——Pascal&#39;s Triangle II

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

  3. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  4. 【Leetcode】Pascal&#39;s Triangle II

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

  5. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  6. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  7. leetcode:Pascal&#39;s Triangle

    一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...

  8. Pascal&#39;s Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...

  9. Pascal&#39;s Triangle II

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

随机推荐

  1. arm-linux-gcc下载与安装

    在RHEL 5平台上安装配置arm-linux-gcc  2011-02-23 19:35:40|  分类: 嵌入式开发环境 |  标签: |字号大中小 订阅 . 在linux平台上安装好的基础上,开 ...

  2. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  3. 安装Oracle时可能碰到的常见问题-1

    安装Oracle可能有些人觉得是一件非常easy的事情,但事实上是在安装的过程中蕴含着丰富的知识点.尤其安装在Linux平台,可能会碰到这样或那样各种诡异的问题,透过问题看到本质,这才是从深处理解Or ...

  4. JS - 图片放大器

    下载地址:http://www.lanrentuku.com/js/tupian-1170.html

  5. 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API

    原文 [ASP.NET Web API教程]2.1 创建支持CRUD操作的Web API 2.1 Creating a Web API that Supports CRUD Operations2.1 ...

  6. System Request 进入KDB模式过程详解

    0   echo g > /proc/sysrq-trigger   怎么让系统停下来,进入进入KDB循环? 1   需要简单了解下:Linux Magic System Request 2   ...

  7. 百度贴吧客户端(Android)网络通信行为分析

    百度贴吧安卓客户端网络通信行为分析 本文由CSDN-蚍蜉撼青松[主页:http://blog.csdn.net/howeverpf]原创,转载请注明出处! 一.实验环境与结果概述 1.1 实验环境   ...

  8. 【Unity3D自学记录】Unity3D网络之Socket聊天室初探

    首先创建一个服务端程序,这个程序就用VS的控制台程序做即可了. 代码例如以下: using System; using System.Collections.Generic; using System ...

  9. 14.3.2.3 Consistent Nonlocking Reads 一致性非锁定读

    14.3.2.3 Consistent Nonlocking Reads 一致性非锁定读 一致性读 意味着 InnoDB 使用多版本来保护查询一个数据库在当前时间点的快照. 查询看到被事务做出的修改, ...

  10. HTTP POST请求的Apache Rewrite规则设置

    最近自测后端模块时有个业务需求需要利用WebServer(我用的是Apache)将HTTP POST请求转发至后端C模块,后端处理后返回2进制加密数据.http post请求的url格式为:     ...