-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet5.cpp
More file actions
59 lines (56 loc) · 1.7 KB
/
Copy pathleet5.cpp
File metadata and controls
59 lines (56 loc) · 1.7 KB
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
55
56
57
58
59
#include <string>
class Solution {
public:
string longestPalindrome(string s) {
int len = s.size() ;
int res = 1 ;
int start = 0 ;
//jishu
for(int mid=0 ;mid<len ;mid++){
int st, end ;
for(st=mid-1,end=mid+1 ;st>=0 && end<len ;st--,end++){
if(s[st] != s[end]){
if(end-st-1>res){
res = end-st+1-2 ;
start = st+1 ;
//cout << " " << mid << ", "<<st << ", " <<res << ", " << s.substr(start, res) << endl ;
}
break ;
}else{
if(st==0 || end==len-1){
if (end-st+1 > res){
res = end-st+1 ;
start = st ;
}
break ;
}
}
}
}
//cout <<"answer : " << start << ", " <<res << endl ;
//oushu
for(int mid=0 ;mid<len ;mid++){
int st, end ;
for(st=mid, end=mid+1 ; st>=0 && end<len;st--, end++){
if(s[st] != s[end]){
if(end-st-1 > res){
res = end-st+1-2 ;
start = st+1 ;
//cout << " " << mid << ", "<<st << ", " <<res << ", " << s.substr(start,res)<< endl ;
}
break ;
}else{
if(st==0 || end==len-1){
if (end-st+1 > res){
res = end-st+1 ;
start = st ;
}
break ;
}
}
}
}
//cout <<"answer : " << start << ", " <<res << endl ;
return s.substr(start,res) ;
}
};