-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZigZag_Conversion.c
112 lines (106 loc) · 3.1 KB
/
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
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
#define UP 1
#define DOWN 2
char* convert(char* s, int numRows) {
int len = strlen(s);
char *rZigZag = NULL;
int *MoreData = NULL;
int *Offset = NULL;
int i = 0;
int j = 0;
int counter = 0;
int Index = 0;
int Dir = DOWN;
rZigZag = malloc((len + 1) * sizeof(char));
memset(rZigZag, 0, len * sizeof(char));
MoreData = malloc(numRows * sizeof(int));
memset(MoreData, 0, numRows * sizeof(int));
Offset = malloc(numRows * sizeof(int));
memset(Offset, 0, numRows * sizeof(int));
rZigZag[0] = s[0];
if (len < 3 || numRows < 2 || numRows >= len)
return s;
i = 0;
counter = 0;
while (len > i) {
//printf("counter %d\n", counter);
if (Dir == DOWN) {
//printf("i %d more data index %d\n", i, counter);
MoreData[counter]++;
if (counter == (numRows - 1)){
if (numRows > 2) {
Dir = UP;
counter = 1;
}
else{
Dir = DOWN;
counter = 0;
}
}
else
counter++;
}
else {
//printf("i %d more data index %d\n", i, numRows - 1 - counter);
MoreData[numRows -1 - counter]++;
if (counter == numRows - 2) {
Dir = DOWN;
counter = 0;
}
else
counter++;
}
i++;
}
int sum = MoreData[0];
MoreData[0] = 0;
//printf("i %d : %d\n",0, MoreData[0]);
int temp = 0;
for (i = 1; i < numRows; i++){
temp = MoreData[i];
MoreData[i] = sum;
//printf("i %d : %d\n",i, MoreData[i]);
sum += temp;
}
i = 0;
counter = 0;
Dir = DOWN;
while (len > i) {
//printf("s[%d]%c\n",i,s[i]);
if (Dir == DOWN) {
// printf("zigzag index %d assgin %c s[%d]\n", Offset[counter] + MoreData[counter], s[i], i);
rZigZag[Offset[counter] + MoreData[counter ]] = s[i];
//printf("row %d \n", counter);
Offset[counter]++;
if (counter == (numRows - 1)){
if (numRows > 2) {
Dir = UP;
counter = 1;
}
else{
Dir = DOWN;
counter = 0;
}
}
else
counter++;
}
else {
//printf("UP zigzag index %d more data assgin %c s[%d]\n",
// Offset[numRows -1 - counter] + MoreData[numRows -1 - counter], s[i], i);
rZigZag[Offset[numRows -1 - counter] + MoreData[numRows -1 - counter]] = s[i];
//printf("row %d \n", numRows -1 - counter);
Offset[numRows -1 - counter]++;
if (counter == numRows - 2) {
Dir = DOWN;
counter = 0;
}
else
counter++;
}
i++;
}
//for (i = 0; i < len; i++)
// printf("%c", rZigZag[i]);
rZigZag[len] = 0;
return rZigZag;
}