-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.txt
46 lines (33 loc) · 891 Bytes
/
5.txt
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
class Solution {
private:
int maxLen;
int start;
public:
string longestPalindrome(string s)
{
int len = s.size();
if(len < 2 ) return s;
start = maxLen = 0;
for(int i = 0; i < len; i++)
{
valid(s,i,i);
valid(s,i,i+1); //假定是奇数;
}
//cout<<start<<" "<<maxLen<<endl;
return s.substr(start,maxLen);
}
private:
void valid(string &s, int left, int right )
{
while(s[left] == s[right] && left >= 0 && right < s.size())
{
left--;
right++;
}
if(right-left-1 > maxLen)
{
start = left+1;
maxLen = right-left-1;
}
}
};