-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode0005.cpp
32 lines (31 loc) · 947 Bytes
/
Leetcode0005.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
class Solution {
public:
string longestPalindrome(string s) {
int res = 0;
string str;
for (int i = 0; i < s.size(); i ++ )
{
for (int j = 0; i - j >= 0 && i + j < s.size(); j ++ )
if (s[i - j] == s[i + j])
{
if (j * 2 + 1 > res)
{
res = j * 2 + 1;
str = s.substr(i - j, j * 2 + 1);
}
}
else break;
for (int j = i, k = i + 1; j >= 0 && k < s.size(); j -- , k ++ )
if (s[j] == s[k])
{
if (k - j + 1 > res)
{
res = k - j + 1;
str = s.substr(j, k - j + 1);
}
}
else break;
}
return str;
}
};