forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDKhan.cpp
More file actions
26 lines (22 loc) · 713 Bytes
/
Copy pathPDKhan.cpp
File metadata and controls
26 lines (22 loc) · 713 Bytes
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
class Solution {
public:
void compare(string s, int left, int right, int& max_start, int& max_end){
while(left >= 0 && right < s.length() && s[left] == s[right]){
left--;
right++;
}
if(max_end - max_start < right - left - 1){
max_start = left + 1;
max_end = right - 1;
}
}
string longestPalindrome(string s) {
int max_start = 0;
int max_end = 0;
for(int i = 0; i < s.length(); i++){
compare(s, i, i, max_start, max_end);
compare(s, i, i + 1, max_start, max_end);
}
return s.substr(max_start, max_end - max_start + 1);
}
};