Skip to content

Commit 11caedc

Browse files
committed
Update code-c-1.1:左旋转字符串
1、leftshift2算法测试有错,修改后已验证 2、其他算法添加了错误输入判断 3、leftShift3算法与leftShift3雷同,并不包含有递归,没有实际意义
1 parent 5ac7bad commit 11caedc

9 files changed

+237
-520
lines changed

ebook/code/c/1.1:左旋转字符串.c

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ void leftShift1(char * arr, int n)
1515
char tmpChar;
1616
int i, j;
1717

18-
if (tmpLen == 0)
19-
return ;
18+
if (tmpLen == 0)
19+
return ;
2020

2121
if (n >= 0)
2222
{
2323
n %= tmpLen;
24-
for (i = 0; i < n; i++)
24+
for (i = 0; i < n; i++)
2525
{
2626
tmpChar = *arr;
2727
for (j = 0; j < tmpLen - 1; j++)
@@ -33,8 +33,8 @@ void leftShift1(char * arr, int n)
3333
}
3434
else
3535
{
36-
n = -((-n) % tmpLen)
37-
for (i = 0; i < -n; i++)
36+
n = -((-n) % tmpLen);
37+
for (i = 0; i < -n; i++)
3838
{
3939
tmpChar = *(arr + tmpLen - 1);
4040
for (j = tmpLen - 1; j > 0; j--)
@@ -54,28 +54,28 @@ void leftShift2(char * arr, int len, int n)
5454
int p0 = 0, p1;
5555
char tmpChar;
5656

57-
if (n == 0)
58-
return ;
57+
if (n == 0)
58+
return ;
5959

60-
if (n < 0)
61-
n = len - (-1 * n) % len;
62-
n %= len;
63-
if (n == 0)
64-
return ;
60+
if (n < 0)
61+
n = len - (-1 * n) % len;
62+
n %= len;
63+
if (n == 0)
64+
return ;
6565

66-
p1 = n;
66+
p1 = n;
6767

6868
/* O(m - n - k) k is the last section*/
6969
while (p1 + n - 1 < tmpLen)
7070
{
71-
for (i = 0; i < n; ++i)
72-
{
73-
tmpChar = *(arr + p0);
74-
*(arr + p0) = *(arr + p1);
75-
*(arr + p1) = tmpChar;
76-
p0++;
77-
p1++;
78-
}
71+
for (i = 0; i < n; ++i)
72+
{
73+
tmpChar = *(arr + p0);
74+
*(arr + p0) = *(arr + p1);
75+
*(arr + p1) = tmpChar;
76+
p0++;
77+
p1++;
78+
}
7979
}
8080
/*
8181
* not good O(k * (n + k)) k = tmpLen - p1
@@ -130,16 +130,16 @@ void leftShift4(char * arr, int len, int n)
130130
int p0 = 0, p1;
131131
char tmpChar;
132132

133-
if (len == 0)
134-
return ;
133+
if (len == 0)
134+
return ;
135135

136-
if (n < 0)
137-
n = len - (-1 * n) % len;
138-
n %= len;
139-
if (n == 0)
140-
return ;
136+
if (n < 0)
137+
n = len - (-1 * n) % len;
138+
n %= len;
139+
if (n == 0)
140+
return ;
141141

142-
p1 = n;
142+
p1 = n;
143143

144144
/* O(m - n - k) k is the last section*/
145145
while (p1 < tmpLen)
@@ -173,17 +173,17 @@ void myinvert(char * start, char * end)
173173
//翻转法
174174
void leftShift5(char * arr, int len, int n)
175175
{
176-
if (len == 0)
177-
return ;
176+
if (len == 0)
177+
return ;
178178

179-
if (n < 0)
180-
n = len - (-1 * n) % len;
181-
n %= len;
179+
if (n < 0)
180+
n = len - (-1 * n) % len;
181+
n %= len;
182182

183-
if (n == 0)
184-
return ;
183+
if (n == 0)
184+
return ;
185185

186-
myinvert(arr, arr + n - 1);
186+
myinvert(arr, arr + n - 1);
187187
myinvert(arr + n, arr + len - 1);
188188
myinvert(arr, arr + len - 1);
189189
}
@@ -202,21 +202,21 @@ int gcd(int m, int n)
202202
//循环移位法
203203
void leftShift6(char * arr, int len, int n)
204204
{
205-
int group, x, i, j;
206-
char tmpChar;
205+
int group, x, i, j;
206+
char tmpChar;
207207

208-
if (len == 0)
209-
return ;
208+
if (len == 0)
209+
return ;
210210

211-
if (n < 0)
212-
n = len - (-1 * n) % len;
213-
n %= len;
211+
if (n < 0)
212+
n = len - (-1 * n) % len;
213+
n %= len;
214214

215-
if (n == 0)
216-
return ;
215+
if (n == 0)
216+
return ;
217217

218-
group = gcd(len, n);
219-
x = len / group;
218+
group = gcd(len, n);
219+
x = len / group;
220220

221221
for (i = 0; i < group; i++)
222222
{
@@ -228,72 +228,72 @@ void leftShift6(char * arr, int len, int n)
228228
*(arr + (i + (j * n)) % len) = tmpChar;
229229
}
230230
}
231+
231232
int main()
232233
{
233-
char str[50];
234-
sprintf(str, "123456");
235-
printf("The origin str is :%s\tlen is :%zu\n", str, strlen(str));
236-
printf("\n");
237-
leftShift1(str, 2);
238-
printf("The leftShift1 str is :%s\n", str);
239-
leftShift1(str, -2);
240-
printf("The leftShift1 str is :%s\n", str);
241-
leftShift1(str, 3);
242-
printf("The leftShift1 str is :%s\n", str);
243-
leftShift1(str, strlen(str) - 3);
244-
printf("The leftShift1 str is :%s\n", str);
245-
printf("\n");
246-
247-
leftShift2(str, strlen(str), 3);
248-
printf("The leftShift2 str is :%s\n", str);
249-
leftShift2(str, strlen(str), strlen(str) - 3);
250-
printf("The leftShift2 str is :%s\n", str);
251-
leftShift2(str + 2, strlen(str) - 2, 2);
252-
printf("The leftShift2 str is :%s\n", str);
253-
leftShift2(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
254-
printf("The leftShift2 str is :%s\n", str);
255-
printf("\n");
256-
257-
258-
leftShift3(str, strlen(str), 3);
259-
printf("The leftShift3 str is :%s\n", str);
260-
leftShift3(str, strlen(str), strlen(str) - 3);
261-
printf("The leftShift3 str is :%s\n", str);
262-
leftShift3(str + 2, strlen(str) - 2, 2);
263-
printf("The leftShift3 str is :%s\n", str);
264-
leftShift3(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
265-
printf("The leftShift3 str is :%s\n", str);
266-
printf("\n");
267-
268-
leftShift4(str, strlen(str), 3);
269-
printf("The leftShift4 str is :%s\n", str);
270-
leftShift4(str, strlen(str), strlen(str) - 3);
271-
printf("The leftShift4 str is :%s\n", str);
272-
leftShift4(str + 2, strlen(str) - 2, 2);
273-
printf("The leftShift4 str is :%s\n", str);
274-
leftShift4(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
275-
printf("The leftShift4 str is :%s\n", str);
276-
printf("\n");
277-
278-
leftShift5(str, strlen(str), 3);
279-
printf("The leftShift5 str is :%s\n", str);
280-
leftShift5(str, strlen(str), strlen(str) - 3);
281-
printf("The leftShift5 str is :%s\n", str);
282-
leftShift5(str + 2, strlen(str) - 2, 2);
283-
printf("The leftShift5 str is :%s\n", str);
284-
leftShift5(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
285-
printf("The leftShift5 str is :%s\n", str);
286-
printf("\n");
287-
288-
leftShift6(str, strlen(str), 3);
289-
printf("The leftShift6 str is :%s\n", str);
290-
leftShift6(str, strlen(str), strlen(str) - 3);
291-
printf("The leftShift6 str is :%s\n", str);
292-
leftShift6(str + 2, strlen(str) - 2, 2);
293-
printf("The leftShift6 str is :%s\n", str);
294-
leftShift6(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
295-
printf("The leftShift6 str is :%s\n", str);
296-
printf("\n");
297-
298-
return 0;
234+
char str[50];
235+
sprintf(str, "123456");
236+
printf("The origin str is :%s\tlen is :%zu\n", str, strlen(str));
237+
printf("\n");
238+
leftShift1(str, 2);
239+
printf("The leftShift1 str is :%s\n", str);
240+
leftShift1(str, -2);
241+
printf("The leftShift1 str is :%s\n", str);
242+
leftShift1(str, 3);
243+
printf("The leftShift1 str is :%s\n", str);
244+
leftShift1(str, strlen(str) - 3);
245+
printf("The leftShift1 str is :%s\n", str);
246+
printf("\n");
247+
248+
leftShift2(str, strlen(str), 3);
249+
printf("The leftShift2 str is :%s\n", str);
250+
leftShift2(str, strlen(str), strlen(str) - 3);
251+
printf("The leftShift2 str is :%s\n", str);
252+
leftShift2(str + 2, strlen(str) - 2, 2);
253+
printf("The leftShift2 str is :%s\n", str);
254+
leftShift2(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
255+
printf("The leftShift2 str is :%s\n", str);
256+
printf("\n");
257+
258+
leftShift3(str, strlen(str), 3);
259+
printf("The leftShift3 str is :%s\n", str);
260+
leftShift3(str, strlen(str), strlen(str) - 3);
261+
printf("The leftShift3 str is :%s\n", str);
262+
leftShift3(str + 2, strlen(str) - 2, 2);
263+
printf("The leftShift3 str is :%s\n", str);
264+
leftShift3(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
265+
printf("The leftShift3 str is :%s\n", str);
266+
printf("\n");
267+
268+
leftShift4(str, strlen(str), 3);
269+
printf("The leftShift4 str is :%s\n", str);
270+
leftShift4(str, strlen(str), strlen(str) - 3);
271+
printf("The leftShift4 str is :%s\n", str);
272+
leftShift4(str + 2, strlen(str) - 2, 2);
273+
printf("The leftShift4 str is :%s\n", str);
274+
leftShift4(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
275+
printf("The leftShift4 str is :%s\n", str);
276+
printf("\n");
277+
278+
leftShift5(str, strlen(str), 3);
279+
printf("The leftShift5 str is :%s\n", str);
280+
leftShift5(str, strlen(str), strlen(str) - 3);
281+
printf("The leftShift5 str is :%s\n", str);
282+
leftShift5(str + 2, strlen(str) - 2, 2);
283+
printf("The leftShift5 str is :%s\n", str);
284+
leftShift5(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
285+
printf("The leftShift5 str is :%s\n", str);
286+
printf("\n");
287+
288+
leftShift6(str, strlen(str), 3);
289+
printf("The leftShift6 str is :%s\n", str);
290+
leftShift6(str, strlen(str), strlen(str) - 3);
291+
printf("The leftShift6 str is :%s\n", str);
292+
leftShift6(str + 2, strlen(str) - 2, 2);
293+
printf("The leftShift6 str is :%s\n", str);
294+
leftShift6(str + 2, strlen(str) - 2, strlen(str) - 2 - 2);
295+
printf("The leftShift6 str is :%s\n", str);
296+
printf("\n");
297+
298+
return 0;
299299
}

0 commit comments

Comments
 (0)