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

帕斯卡三角,很简单的问题,见代码:

 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> ret;
 5         vector<int> tmpVec;
 6         ret.clear();
 7         tmpVec.clear();
 8         for(int i = 0; i < numRows; ++i){
 9             if(i == 0){
10                 tmpVec.push_back(1);
11             }else{
12                 for(int j = 0; j <= i; ++j){
13                     if(j == 0) tmpVec.push_back(1);
14                     else if(j == i) tmpVec.push_back(1);
15                     else tmpVec.push_back(ret[i - 1][j - 1] + ret[i - 1][j]);
16                 }
17             }
18             ret.push_back(tmpVec);
19             tmpVec.clear();
20         }
21         return ret;
22     }
23 };

转载于:https://www.cnblogs.com/-wang-cheng/p/4918826.html

LeetCode OJ:Pascal's Triangle(帕斯卡三角)相关推荐

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

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

  2. 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 ...

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

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

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

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

  5. LeetCode 118. Pascal’s Triangle

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

  6. LeetCode 119. Pascal’s Triangle II

    119. Pascal's Triangle II My Submissions QuestionEditorial Solution Total Accepted: 72147 Total Subm ...

  7. LeetCode 34 Pascal's Triangle

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

  8. Pascal's Triangle (LeetCode) 帕斯卡三角或者叫杨辉三角----动态规划和memoization

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  9. LeetCode OJ:Pascal's TriangleII(帕斯卡三角II)

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

最新文章

  1. android饼状图简书,Charts-饼状图
  2. EBB-11、Linux启动流程
  3. 用74LS161构建多位计数器Multisim仿真实验
  4. Linux的phpize添加php扩展
  5. 网站使用CloudFlare
  6. 原来还有dynamic这东西。
  7. C++编程思想:继承与虚函数以及多态
  8. hydra mysql 爆破_Hydra(爆破神器)使用方法
  9. Opencv将处理后的视频保存出现的问题
  10. 建模元件有哪些在MapleSim中
  11. java.security.InvalidKeyException: Illegal key size
  12. Unity3D的四种坐标系
  13. 全局中断_【安全圈】微软更新造成Office 365等多个在线服务中断!
  14. 2021-09-02spark streaming
  15. ubuntu安装gcc失败怎么办?
  16. Java项目大合集练手项目经验
  17. 射击类游戏html代码,超简单射击游戏
  18. 关于我学前端一年的体验(心得)
  19. 语音输入转文字怎么操作?分享几种语音转文字技巧
  20. 超精细写实的3D人物模型,这可不是照片!

热门文章

  1. android模块编译错误,android studio编译出错:Android resource linking failed
  2. mysql支持ASCII_MySQLASCII()函数返回字符的ASCII码值
  3. Wordpress婚庆婚纱摄影工作室企业网站主题模板
  4. 发货100全功能网站/绿色版
  5. SpringCloud Hoxton版微服务- Ribbon实现负载均衡
  6. 上海交大MBA学费与资助
  7. 用C#读取XML文档
  8. iOS7应用开发4、Foundation框架
  9. Python_排序算法实现
  10. BrainFuck——C实现BrainFuck解释器