Pascal's Triangle
帕斯卡三角形,主要考察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相关推荐
- 帕斯卡三角形(Pascal's triangle)
// The following code is compiled on VC2005 // #include "stdafx.h" /*--------------------- ...
- [leedcode 118] Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Leetcode 118:Pascal's Triangle 杨辉三角
118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pasca ...
- 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 ...
- LeetCode Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 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 ...
- 【LeetCode从零单排】No118 Pascal's Triangle
题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- [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 ...
- C++Pascal‘s Triangle杨辉三角的实现算法
C++Pascal's Triangle杨辉三角的实现算法 C++Pascal's Triangle杨辉三角的实现算法完整源码(定义,实现,main函数测试) C++Pascal's Triangle ...
- Pascal's Triangle Leetcode Java and C++
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
最新文章
- Java如何清除日期_Java中关于日期的处理方法
- iOS - UIStoryboard
- AVR系列之ADC转换
- Node.js- sublime搭建node的编译环境
- AD 域服务简介(二)- Java 获取 AD 域用户
- 不会Nacos的配置中心?你怎么敢的呀!如何一步步实现Nacos作为服务的配置中心(全网最详系列)
- 从零开始学习docker(二)网络
- 计算机组成原理与汇编语言试题,中央电大计算机组成原理与汇编语言试题
- 利用DataSnap的回调功能在客户端显示服务器方法的执行进度
- axis2生成客户端代码_利用ApiPost一键、快速生成接口文档!女猿也过38节!
- word python api_WordNet Python API (整理总结)
- 基于机器学习与BERT的在线招聘欺诈检测平台
- Linux下测试PHP和MySQL是否正确安装
- c#自带类实现的多文件压缩和解压
- DSkinControl( For MFC) 控件- dsCtrlComboBox
- java做一个客房管理系统定制_JAVA+MySQL酒店客房管理系统
- 多频子量子计算机,量子计算机研究:纠错和容错计算
- Codeforces918 D. MADMAX(博弈,记忆化搜索)
- poi导出excel在单元格内画斜线
- php图片的编码是什么,jpeg是什么图像压缩编码标准