-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dennaolan.cpp
52 lines (42 loc) · 1.1 KB
/
Dennaolan.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
43
44
45
46
47
48
49
50
51
52
class Solution {
public:
string longestPalindrome(string s) {
int l, r;
bool enter;
string ans;
for ( int i = 0 ; i < s.size() ; i++ ) {
enter = false;
for ( l = i-1, r = i+1 ; l >= 0 && r < s.size() ; l--, r++ ) {
enter = true;
if ( s[l] != s[r] )
break;
}
l++;
r--;
if ( !enter || l == r ) {
if ( ans.size() == 0 )
ans = s[i];
}
else
ans = s.substr( l, r-l+1 ).size() > ans.size() ? s.substr( l, r-l+1 ) : ans;
}
for ( int i = 0 ; i < s.size() ; i++ ) {
enter = false;
for ( l = i, r = i+1 ; l >= 0 && r < s.size() ; l--, r++ ) {
enter = true;
if ( s[l] != s[r] )
break;
}
l++;
r--;
if ( !enter || l > r ) {
if ( ans.size() == 0 )
ans = s[i];
}
else
ans = s.substr( l, r-l+1 ).size() > ans.size() ? s.substr( l, r-l+1 ) : ans;
}
return ans;
}
private:
};