帕斯卡三角形,主要考察vector的用法。

vector<vector<int> > generate(int numRows){vector<vector<int> > result;vector<int> tmp;result.clear();tmp.clear();int i,j;if(numRows == 0)return result;else if(numRows == 1){tmp.push_back(1);result.push_back(tmp);return result;}else if(numRows == 2){tmp.push_back(1);result.push_back(tmp);tmp.push_back(1);result.push_back(tmp);return result;}else{tmp.push_back(1);result.push_back(tmp);tmp.push_back(1);result.push_back(tmp);for(i = 2; i < numRows; i++){tmp.clear();            tmp.push_back(1);for(j = 1;j<=i-1;j++){tmp.push_back(result[i-1][j-1]+result[i-1][j]);}tmp.push_back(1);result.push_back(tmp);            }}return result;
}

上次怎么写这个麻烦,第二次写得简练许多

 1     vector<vector<int> > generate(int numRows){
 2         vector<vector<int> > res;
 3         vector<int> tmp, last;
 4         if(numRows == 0)
 5             return res;
 6         int i,j;
 7         for(i = 0; i < numRows; i++){
 8             tmp = vector<int>(i+1, 0);
 9             tmp[0] = 1;
10             tmp[i] = 1;
11             for(j = 1; j < i; j++)
12                 tmp[j] = last[j-1] + last[j];
13             res.push_back(tmp);
14             last = tmp;
15         }
16         return res;
17     }

转载于:https://www.cnblogs.com/waruzhi/p/3317753.html

Pascal's Triangle相关推荐

  1. 帕斯卡三角形(Pascal's triangle)

    // The following code is compiled on VC2005 // #include "stdafx.h" /*--------------------- ...

  2. [leedcode 118] Pascal's Triangle

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

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

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

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

  5. LeetCode Pascal's Triangle

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

  6. 119. Pascal's Triangle II (Graph; WFS)

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

  7. 【LeetCode从零单排】No118 Pascal's Triangle

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

  8. [Array]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 ...

  9. C++Pascal‘s Triangle杨辉三角的实现算法

    C++Pascal's Triangle杨辉三角的实现算法 C++Pascal's Triangle杨辉三角的实现算法完整源码(定义,实现,main函数测试) C++Pascal's Triangle ...

  10. Pascal's Triangle Leetcode Java and C++

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

最新文章

  1. Java如何清除日期_Java中关于日期的处理方法
  2. iOS - UIStoryboard
  3. AVR系列之ADC转换
  4. Node.js- sublime搭建node的编译环境
  5. AD 域服务简介(二)- Java 获取 AD 域用户
  6. 不会Nacos的配置中心?你怎么敢的呀!如何一步步实现Nacos作为服务的配置中心(全网最详系列)
  7. 从零开始学习docker(二)网络
  8. 计算机组成原理与汇编语言试题,中央电大计算机组成原理与汇编语言试题
  9. 利用DataSnap的回调功能在客户端显示服务器方法的执行进度
  10. axis2生成客户端代码_利用ApiPost一键、快速生成接口文档!女猿也过38节!
  11. word python api_WordNet Python API (整理总结)
  12. 基于机器学习与BERT的在线招聘欺诈检测平台
  13. Linux下测试PHP和MySQL是否正确安装
  14. c#自带类实现的多文件压缩和解压
  15. DSkinControl( For MFC) 控件- dsCtrlComboBox
  16. java做一个客房管理系统定制_JAVA+MySQL酒店客房管理系统
  17. 多频子量子计算机,量子计算机研究:纠错和容错计算
  18. Codeforces918 D. MADMAX(博弈,记忆化搜索)
  19. poi导出excel在单元格内画斜线
  20. php图片的编码是什么,jpeg是什么图像压缩编码标准

热门文章

  1. 商淘多b2b2c商城系统怎么在个人电脑上安装_社交电商系统开发是否有价值?
  2. linux的veth导致网络不通,linux的veth对网桥通信实验
  3. oracle 条件反转,Oracle反转倒置函数
  4. oracle plsql开启并行,Oracle开启并行的几种方法
  5. Angular 服务
  6. IDEA快捷键及基本使用方法
  7. 编写五子棋的完整python代码_python制作简单五子棋游戏
  8. 样式集(八)弹窗,规则弹窗,半透明弹窗
  9. Swift 换肤方案的实现使用SwiftTheme框架
  10. 视频编解码之理论概述 和即时通信