118. Pascal's Triangle
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相关推荐
- Leetcode 118:Pascal's Triangle 杨辉三角
118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pasca ...
- LeetCode 118. Pascal’s Triangle
118. Pascal's Triangle My Submissions QuestionEditorial Solution Total Accepted: 80029 Total Submiss ...
- 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 ...
- [leedcode 118] Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- LeetCode - Easy - 118. Pascal‘s Triangle
Topic Array Description https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer ...
- [LeetCode] 118. Pascal's Triangle Java
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- leetcode python3 简单题118. Pascal's Triangle
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十八题 (1)题目 英文: Given a non-negative int ...
- 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 ...
最新文章
- c:redirect标签的使用
- Visual Studio代码中的多个游标[关闭]
- 认识AndEngine选自Android 2D游戏引擎AndEngine快速入门教程
- Pandas的DataFrame输出截断和省略问题
- java设计模式之设计原则③单一职责原则
- 职业:图像处理入门教程
- extjs jquery使用场合
- Idea启动报错 Error:java: System Java Compiler was not found in classpath
- web内容缓存 nginx高性能缓存详解
- 跨域——vue中的axios.post使用json数据传输,出现请求头字段内容类型是不被允许的情况的解决方案
- NetBeans在Apache基金会取得的进展
- python处理时间序列非平稳_非平稳时间序列的自相关
- Python 数据分析与挖掘概述
- 自适应PID控制基本概念及常用自适应算法
- Mac顶部菜单栏(Menubar)卡死
- 58同城一面【前端】
- WPF中任务栏只显示主窗口
- 不用找,你想要的游戏3d纹理图片素材都在这里
- ceph节点journal盘更换
- python与炒股_Python量化炒股入门与实战技巧