-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path118-pascals-triangle.cpp
More file actions
63 lines (54 loc) · 1.42 KB
/
Copy path118-pascals-triangle.cpp
File metadata and controls
63 lines (54 loc) · 1.42 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
// 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]
// ]
//
// Tags: Array
//
// https://leetcode.com/problems/pascals-triangle/
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
for(int i = 0; i< numRows; i++){
vector<int> row;
for(int j = 0; j <= i; j++){
if(j == 0 || j == i){
row.push_back(1);
}else{
row.push_back(result[i-1][j-1] + result[i-1][j]);
}
}
result.push_back(row);
}
return result;
}
};
TEST(leetcode_118_pascals_triangle, Basic)
{
Solution *solution = new Solution();
vector<vector<int>> expected = {{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}};
EXPECT_EQ(expected, solution->generate(5));
expected = {};
EXPECT_EQ(expected, solution->generate(0));
expected = {{1}};
EXPECT_EQ(expected, solution->generate(1));
expected = {{1}, {1, 1}};
EXPECT_EQ(expected, solution->generate(2));
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}