-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
42 lines (41 loc) · 1015 Bytes
/
solution.cpp
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
class Solution
{
public:
string longestPalindrome(string s)
{
int position, maxLength=0, length=0;
for(int i=0;i<s.size();i++)
{
length = 1;
for(int j=1;i+j<s.size()&&i-j>=0;j++)
{
if(s[i-j] == s[i+j])
length += 2;
else
break;
}
if(length > maxLength)
{
maxLength = length;
position = i-length/2;
}
}
for(int i=0;i+1<s.size();i++)
{
length = 0;
for(int j=0;i+1+j<s.size()&&i-j>=0;j++)
{
if(s[i-j] == s[i+1+j])
length += 2;
else
break;
}
if(length > maxLength)
{
maxLength = length;
position = i-length/2+1;
}
}
return s.substr(position,maxLength);
}
};