File tree 1 file changed +11
-10
lines changed
scripts/algorithms/P/Print Words Vertically
1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 0 ms (Top 100.00%) | Memory: 6.4 MB (Top 78.82%)
1
2
class Solution {
2
3
public:
3
4
vector<string> printVertically (string s) {
4
-
5
+
5
6
int row = 0 , col = 0 ;
6
-
7
+
7
8
stringstream sso (s);
8
9
string buffer;
9
-
10
+
10
11
while (sso >> buffer)
11
12
{
12
13
row++;
13
14
col = max ((int )buffer.size (),col);
14
15
}
15
-
16
+
16
17
vector<vector<char >> matrix (row, vector<char >(col,' ' ));
17
-
18
+
18
19
sso = stringstream (s);
19
-
20
+
20
21
int i = 0 ;
21
22
while (sso >> buffer)
22
23
{
23
24
for (int j = 0 ; j < buffer.size (); j++)
24
25
matrix[i][j] = buffer[j];
25
26
i++;
26
27
}
27
-
28
+
28
29
vector<string> res;
29
30
for (int j = 0 ; j < col; j++)
30
31
{
31
32
string item;
32
33
for (int i = 0 ; i < row; i++)
33
34
item.push_back (matrix[i][j]);
34
-
35
+
35
36
for (int i = item.size ()-1 ; i >= 0 ; i--)
36
37
{
37
38
if (item[i] != ' ' )
@@ -42,7 +43,7 @@ class Solution {
42
43
}
43
44
res.push_back (item);
44
45
}
45
-
46
+
46
47
return res;
47
48
}
48
- };
49
+ };
You can’t perform that action at this time.
0 commit comments