-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddALetterIsPalin.c
More file actions
58 lines (50 loc) · 965 Bytes
/
AddALetterIsPalin.c
File metadata and controls
58 lines (50 loc) · 965 Bytes
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
/* 给定一个字符串,问是否能通过添加一个字母将其变为回文串 */
#include <stdio.h>
#include <string.h>
int IsPalin(char *start, char *end)
{
while (start < end)
{
if (*start++ != *end--)
return 0;
}
return 1;
}
int judge(char *start, char *end)
{
if (start >= end)
return 0;
//判断是否可以通过在左边添加一个字母使其成为回文串
if (IsPalin(start, end - 1))
return 1;
//判断是否可以通过在右边添加一个字母使其成为回文串
else if (IsPalin(start + 1, end))
return 1;
else //在中间加
{
while (start < end)
{
if (*start++ != *end--)
{
if (IsPalin(start, end + 1))
return 1;
else if (IsPalin(start - 1, end))
return 1;
else
return 0;
}
}
}
}
int main()
{
char *arr = "abcdba";
int len = strlen(arr);
char *end = arr + len - 1;
int ret = judge(arr, end);
if (ret)
printf("yes\n");
else
printf("no\n");
return 0;
}