-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello.cpp
95 lines (85 loc) · 2.4 KB
/
hello.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
private:
static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
vector<int> ç(vector<vector<int>> &matrix)
{
if (!matrix.size())
{
return {};
}
if (!matrix.at(0).size())
{
return {};
}
unsigned long rows{matrix.size()};
unsigned long columns{matrix.at(0).size()};
vector<vector<bool>> visited(rows, vector<bool>(columns, false));
vector<int> order(rows * columns);
int row{0};
int column{0};
int direction_index{0};
for (int i{0}; i < rows * columns; ++i)
{
order.at(i) = matrix.at(row).at(column);
visited.at(row).at(column) = true;
int next_row{row + directions[direction_index][0]};
int next_column{column + directions[direction_index][1]};
if (next_row < 0 || next_row >= rows || next_column < 0 || next_column >= columns || visited.at(next_row).at(next_column))
{
direction_index = (direction_index + 1) % 4;
}
row = {row + directions[direction_index][0]};
column = {column + directions[direction_index][1]};
}
return order;
}
};
class Solution
{
public:
vector<vector<int>> generate(int numRows)
{
vector<vector<int>> result(numRows);
for (int row{0}; row < numRows; ++row)
{
vector<int> temp(row+1);
for (int column{0}; column < row; ++column)
{
int first_row = row - 1;
if (first_row < 0 || column == 0 || column ==row-1 )
{
temp.at(column)=0;
}
else
{
temp.at(column) = result.at(row-1).at(column-1) + result.at(row-1).at(column);
}
}
result.at(row) = temp;
}
return result;
}
};
int test_spiralOrder()
{
vector<vector<int>> matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
spiralOrder s;
vector<int> result = s.spiralOrder(matrix);
for (int i = 0; i < result.size(); i++)
{
std::cout << result.at(i) << " ";
}
std::cout << std::endl;
return 0;
}
int main()
{
Solution s;
vector<vector<int>> resut{s.generate(5)};
return 0;
}