-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path45_MatrixSpiral.cpp
177 lines (153 loc) · 4.13 KB
/
45_MatrixSpiral.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// For this challenge you will print a 2D matrix in spiral order.
/*
have the function MatrixSpiral(strArr) read the array of strings stored in strArr which will represent a 2D N matrix, and your program should return the elements after printing them in a clockwise, spiral order. You should return the newly formed list of elements as a string with the numbers separated by commas. For example: if strArr is "[1, 2, 3]", "[4, 5, 6]", "[7, 8, 9]" then this looks like the following 2D matrix:
1 2 3
4 5 6
7 8 9
1 2 3 5
4 5 6 8
7 8 9 4
1 2 3 6
4 5 6 1
7 8 9 2
So your program should return the elements of this matrix in a clockwise, spiral order which is: 1,2,3,6,9,8,7,4,5
*/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
/*
only analyze number characters
take the string data and create a new list of only numbers to better iterate later on
check for the total numbers to help with the loop
start on row 1 col 1 and iterate to right
once you reach end of col update rows
if is not first row take the last col value
once you reach end row iterate to left
update first and last rows again
now repeat process but in reverse
*/
string MatrixSpiral(string strArr[], int size)
{
vector <vector <int> > list(size);
string temp;
int num;
// Loop to create a new list of ints
for (int row = 0; row < size; row++)
{
for (int col = 0; col < strArr[row].length(); col++)
{
// Condition to convert the string to int and add to the list
if (strArr[row][col] == ',' || strArr[row][col] == ']')
{
istringstream(temp) >> num;
list[row].push_back(num);
temp.clear();
}
// Create the string number
else if (strArr[row][col] >= '0' & strArr[row][col] <= '9')
{
temp += strArr[row][col];
}
}
}
// We provide the total amount of numbers in the matrix.
int total = size * list[0].size();
int start = 0;
// Will represent the parent row and col
int first = 0;
int last = size;
// Will represent the child row and col
// These are our current location
int row = 0;
int col = 0;
// Will represent the length our columns are when analyzed
int colStart = 0;
int colEnd = list[0].size();
// Signals to determine how we iterate
bool down, up;
string result;
// Spiral cycle
while (start < total)
{
// When we are dealing with the parent first row
// We iterate to the right
if (row == first)
{
stringstream convert;
convert << list[row][col];
result += convert.str();
result += ',';
// Condition to check if we have reached the current column end
// Iterate to the right
if (col + 1 < colEnd)
{
col++;
}
else
{
// Update our last row and col end
// Also signal that we now go down
last--;
row++;
colEnd--;
down = true;
up = false;
}
}
// Condition when we reach the parent last row
else if (row == last)
{
stringstream convert;
convert << list[row][col];
result += convert.str();
result += ',';
// Iterate to the left
if (col - 1 >= colStart)
{
col--;
}
else
{
// Update our first row and column start
// Signal that we now go up
first++;
row--;
colStart++;
up = true;
down = false;
}
}
else if (down) // Going down we just take the last value of that row based on column length we set up
{
stringstream convert;
convert << list[row][col];
result += convert.str();
result += ',';
row++;
}
else if (up) // Going down we take the first value of that row based on column lenght we set up
{
stringstream convert;
convert << list[row][col];
result += convert.str();
result += ',';
row--;
}
start++;
}
// Erasing the comma at the end
result.erase(result.end() - 1);
return result;
}
int main()
{
string A[] = { "[1, 2, 3]", "[4, 5, 6]", "[7, 8, 9]" };
string B[] = { "[1, 2]", "[10, 14]" };
string C[] = { "[4, 5, 6, 5]", "[1, 1, 2, 2]", "[5, 4, 2, 9]" };
cout << MatrixSpiral(A, sizeof(A)/sizeof(A[0])) << endl; // 1,2,3,6,9,8,7,4,5
cout << MatrixSpiral(B, sizeof(B) / sizeof(B[0])) << endl; // 1,2,14,10
cout << MatrixSpiral(C, sizeof(C) / sizeof(C[0])) << endl; // 4,5,6,5,2,9,2,4,5,1,1,2
return 0;
}