-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-Zigzag-Conversion.cpp
More file actions
39 lines (32 loc) · 956 Bytes
/
6-Zigzag-Conversion.cpp
File metadata and controls
39 lines (32 loc) · 956 Bytes
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
class Solution {
public:
string convert(string s, int numRows) {
if( numRows == 1 )
return s;
char grid[(int)1e3 + 5][(int)1e3 + 5];
memset(grid, '/', sizeof grid);
pair<int,int>cell = {0, 0};
int i = 0, n = s.size();
while( true )
{
if( cell.first == numRows )
{
cell.first -= 2;
cell.second++;
while( cell.first > -1 && i < n )
grid[cell.first--][cell.second++] = s[i++];
}
if( cell.first == -1 )
cell.first += 2;
if( i >= n )
break;
grid[cell.first++][cell.second] = s[i++];
}
string ans;
for( int i = 0; i < 1e3 + 5; i++ )
for( int j = 0; j < 1e3 + 5; j++ )
if( grid[i][j] != '/' )
ans += grid[i][j];
return ans;
}
};