-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode006_ZigZag_Conversion.cpp
More file actions
57 lines (47 loc) · 1.28 KB
/
LeetCode006_ZigZag_Conversion.cpp
File metadata and controls
57 lines (47 loc) · 1.28 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
class Solution {
public:
string convert(string s, int numRows) {
string tem;
int length = s.size();
int num ;
//int remainder ;
if(numRows == 1)
{
return s;
}
if(numRows > 1)
{
num = length/(2*numRows - 2);
//remainder = length%(2*numRows - 2);
}
for(int j = 0; j < length; j = j+2*(numRows-1) )
{
tem += s[j] ;
}
for(int i = 1; i < numRows-1; i++)
{
for(int j = 0; j < num; j++)//3
{
//cout << s[i+j*(2*num-2)] << s[i+(j+1)*(2*num-2)];
tem += s[i+j*(2*numRows-2)];
tem += s[-i+(j+1)*(2*numRows-2)];
}
int m = i+num*(2*numRows-2);
int n = -i+(num+1)*(2*numRows-2);
if(m < length)
{
tem += s[m];
if(n < length)
{
tem += s[n];
}
}
}
for(int j = numRows-1; j < length; j = j+2*(numRows-1) )
{
//cout << s[j] ;
tem += s[j] ;
}
return tem;
}
};