-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path6. ZigZag Conversion.c
63 lines (46 loc) · 1.37 KB
/
6. ZigZag Conversion.c
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
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/