GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/algorithms/cpp/pascalTriangle/pascalTriangle.cpp at master · weimo/leetcode Expand file tree/ Copy pathpascalTriangle.cppMore file actions 77 lines (72 loc) · 1.67 KB / Copy pathpascalTriangle.cppFile metadata and controls77 lines (72 loc) · 1.67 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 // Source : https://oj.leetcode.com/problems/pascals-triangle/ // Author : Hao Chen // Date : 2014-06-18 /********************************************************************************** * * 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] * ] * * **********************************************************************************/ #include <stdlib.h> #include <vector> #include <iostream> using namespace std; vector<vector<int> > generate(int numRows) { vector<vector<int> > pascalTriangle; for (int i=0; i<numRows; i++){ vector<int> v; if (i==0){ v.push_back(1); } else { v.push_back(1); for(int j=0; j<pascalTriangle[i-1].size()-1; j++){ v.push_back(pascalTriangle[i-1][j] + pascalTriangle[i-1][j+1]); } v.push_back(1); } pascalTriangle.push_back(v); } return pascalTriangle; } void printTriangle(vector< vector<int> > pt) { cout << "[" << endl; for(int i=0; i<pt.size(); i++){ for(int space=(pt.size()-i-1); space>=0; space--){ cout << " "; } cout << "["; for(int j=0; j<pt[i].size(); j++){ cout << pt[i][j]; if(j<pt[i].size()-1){ cout << ","; } } cout << "]"; if(i<pt.size()-1){ cout << ","; } cout << endl; } cout << "]" << endl; } int main(int argc, char** argv) { int n = 3; if (argc>1) { n = atoi(argv[1]); } printTriangle(generate(n)); } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |