-
Notifications
You must be signed in to change notification settings - Fork 0
/
0005#longest_palindromic_substring#medium.c
56 lines (55 loc) · 1.36 KB
/
0005#longest_palindromic_substring#medium.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
/*
think how to tell whether a string is a palindrome or not
aba is a palindrome
then, if babab a palindrome?
or babac a palindrome?
and besides figure the palindrome with odd digits
think how to figure a palindrome with even digites such as bb
lastly, if a palindrome has a length of 1000 digits, what to do?
pay attention
you cannot return a local variable
*/
char* longestPalindrome(char* s)
{
int lens = strlen(s);
int i = 0, floor = 0, ceil = 0, maxlen = 1, count = 1, curmax = 1;
while(i < lens - maxlen)
{
if(s[i] == s[i + 1])
{
curmax = 2;
while((i - count) >= 0 && (i + count + 1) < lens && s[i - count] == s[i + count + 1])
{
curmax += 2;
count ++;
}
if(curmax > maxlen)
{
floor = i - count + 1;
ceil = i + count;
maxlen = curmax;
}
count = 1;
curmax = 1;
}
while((i - count) >= 0 && (i + count) < lens && s[i - count] == s[i + count])
{
curmax += 2;
count ++;
}
if(curmax > maxlen)
{
floor = i - count + 1;
ceil = i + count - 1;
maxlen = curmax;
}
count = 1;
curmax = 1;
i ++;
}
char* result = (char*)malloc((ceil - floor + 2) * sizeof(char));
for(i = 0; i < ceil - floor + 1; i ++)
result[i] = s[floor + i];
result[ceil - floor + 1] = '\0';
return result;
}