-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode005.c
54 lines (49 loc) · 971 Bytes
/
leetcode005.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char a[1002];
char* longestPalindrome(char* s) {
if (s[0] == '\0') return s;
int i, left, right, max, length, maxleft, maxright, mark;
i = left = right = max = length = maxleft = maxright = mark = 0;
while (s[i] != '\0') {
if (mark == 0) left = i-1;
else left = i;
right = i+1;
while (s[left] == s[right] && left>=0) {
left--;
right++;
}
if (mark) {
++i;
mark = 0;
length = right-left+1-2;
}
else {
mark = 1;
length = (i-(left+1))*2+1;
}
if (max < length) {
maxleft = ++left;
maxright = --right;
max = length;
}
}
int j=0;
while (maxright>=maxleft) a[j++] = s[maxleft++];
a[j] = '\0';
return a;
}
int main(int argc, char const *argv[])
{
char *s = "babad\0";
char *d = longestPalindrome(s);
printf("%s\n", d);
return 0;
}
// Input: "babad"
// Output: "bab"
// Note: "aba" is also a valid answer.
// Example 2:
// Input: "cbbd"
// Output: "bb"