-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem5.cpp
72 lines (70 loc) · 1.77 KB
/
problem5.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @lc app=leetcode.cn id=5 lang=cpp
*
* [5] 最长回文子串
*
* https://leetcode-cn.com/problems/longest-palindromic-substring/description/
*
* algorithms
* Medium (28.85%)
* Likes: 1853
* Dislikes: 0
* Total Accepted: 203.8K
* Total Submissions: 705.3K
* Testcase Example: '"babad"'
*
* 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
*
* 示例 1:
*
* 输入: "babad"
* 输出: "bab"
* 注意: "aba" 也是一个有效答案。
*
*
* 示例 2:
*
* 输入: "cbbd"
* 输出: "bb"
*
*
*/
// @lc code=start
class Solution {
public:
string longestPalindrome(string s) {
int left_index = 0;
int right_index = 0;
for(int i=0; i<s.size(); i++) {
// i = 1
for(int k=0;;k++) {
const int left = i-k;
const int right = i+k;
if(left<0 || right>s.size() || s[left]!=s[right]){
break;
}
else {
if(right-left > right_index - left_index) {
left_index = left;
right_index = right;
}
}
}
for(int k=0;;k++) {
const int left = i-k;
const int right = i+k+1;
if(left<0 || right>s.size() || s[left]!=s[right]) {
break;
}
else {
if(right-left > right_index - left_index) {
left_index = left;
right_index = right;
}
}
}
}
return string(s.begin()+left_index, s.begin()+right_index+1);
}
};
// @lc code=end