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

分析 第k层有k个元素,每层第一个及最后一个元素值为1,对于(k>2)层,第n(n>1 && n < k)个元素A[k][n] = A[k-1][n-1]+A[k-1][n];
 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> vals;
 5
 6         vals.resize(numRows);
 7
 8         for(int i = 0; i < numRows; i++){
 9             vals[i].resize(i+1);
10             vals[i][0] = 1;
11             vals[i][vals[i].size()-1] = 1;
12             for(int j = 1; j < vals[i].size()-1; j++){
13                 vals[i][j] = vals[i-1][j-1]+vals[i-1][j];
14             }
15         }
16         return vals;
17     }
18 };

转载于:https://www.cnblogs.com/sankexin/p/5863065.html

118. Pascal's Triangle相关推荐

  1. Leetcode 118:Pascal's Triangle 杨辉三角

    118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pasca ...

  2. LeetCode 118. Pascal’s Triangle

    118. Pascal's Triangle My Submissions QuestionEditorial Solution Total Accepted: 80029 Total Submiss ...

  3. No.118 Pascal's Triangle ||

    No.118 Pascal's Triangle || Given an index k, return the kth row of the Pascal's triangle. For examp ...

  4. [leedcode 118] Pascal's Triangle

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

  5. Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

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

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

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

  7. [LeetCode] 118. Pascal's Triangle Java

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

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

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

  9. 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. c:redirect标签的使用
  2. Visual Studio代码中的多个游标[关闭]
  3. 认识AndEngine选自Android 2D游戏引擎AndEngine快速入门教程
  4. Pandas的DataFrame输出截断和省略问题
  5. java设计模式之设计原则③单一职责原则
  6. 职业:图像处理入门教程
  7. extjs jquery使用场合
  8. Idea启动报错 Error:java: System Java Compiler was not found in classpath
  9. web内容缓存 nginx高性能缓存详解
  10. 跨域——vue中的axios.post使用json数据传输,出现请求头字段内容类型是不被允许的情况的解决方案
  11. NetBeans在Apache基金会取得的进展
  12. python处理时间序列非平稳_非平稳时间序列的自相关
  13. Python 数据分析与挖掘概述
  14. 自适应PID控制基本概念及常用自适应算法
  15. Mac顶部菜单栏(Menubar)卡死
  16. 58同城一面【前端】
  17. WPF中任务栏只显示主窗口
  18. 不用找,你想要的游戏3d纹理图片素材都在这里
  19. ceph节点journal盘更换
  20. python与炒股_Python量化炒股入门与实战技巧

热门文章

  1. springmvc使用spring自带日期类型验证
  2. Linux——vim程序编辑器
  3. 【转】hadoop 报 Name node is in safe mode 错
  4. LmgORM项目: 介绍
  5. malloc,calloc和realloc函数
  6. Vue—相关工具—vue-cli
  7. MyBatis多表查询之XML和注解实现(resultMap结果集映射配置数据库字段与实体类属性一一映射)
  8. HelloGitHub
  9. SDNU 1171.合并果子(区间dp)
  10. 【转】shell之for、while、until循环