- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path6.c
62 lines (52 loc) · 1.46 KB
/
6.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char*convert(char*s, intnRows) {
intlen=strlen(s);
char*ret= (char*)malloc(len+1);
inti, j;
j=0; /* index of return string */
/**
* don't know why return s or modify input pointer will cause runtime error,
* so I have to copy a string and try not to modify s. I tried 5 times to
* finally find out what happened, sad for my acceptance rate...
*/
if (nRows==1) {
while (j<len) {
ret[j] =s[j];
j++;
}
ret[len] ='\0';
returnret;
}
intt= (nRows-1) *2;
intshift, offset;
for (i=0; i<nRows; i++) {
offset=0;
shift= (nRows-i-1) *2;
while (j<len&&i+offset<len) {
ret[j] =s[i+offset];
j++;
if (shift==0) {
shift=t;
}
offset+=shift;
if (shift!=t) {
shift=t-shift;
}
}
}
ret[len] ='\0';
returnret;
}
intmain() {
charstr1[] ="PAYPALISHIRING";
charstr2[] ="A";
charstr3[] ="zvmwnuufnnxvloyvgmliuqandlyavfauaosnlnv";
printf("%s\n", convert(str1, 3));
printf("%s\n", convert(str1, 4));
printf("%s\n", convert(str1, 5));
printf("%s\n", convert(str2, 1));
printf("%s\n", convert(str3, 1));
return0;
}