-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.LongestPalindromicSubstring.cpp
More file actions
executable file
·134 lines (128 loc) · 3.59 KB
/
Copy path5.LongestPalindromicSubstring.cpp
File metadata and controls
executable file
·134 lines (128 loc) · 3.59 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
@filename LongestPalindromicSubstring.cpp
@author caonan
@date 2022-04-04 14:50:06
@reference leetcode
@url https://leetcode-cn.com/problems/longest-palindromic-substring/
@brief 给你一个字符串 s,找到 s 中最长的回文子串。
*/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class Solution {
public:
// 中心扩展法
string longestPalindrome(string s) {
string t = "#";
for (const auto& c : s) {
t += c;
t += "#";
}
string longest_str;
for (int i = 1; i < t.length() - 1; i++) {
int left = i;
int right = i;
while (left >= 0 && right < t.length()) {
if (t[left] == t[right]) {
if (longest_str.length() < right - left + 1) {
longest_str = t.substr(left, right - left + 1); // 每次都有拷贝,是否记住下标索引更好点?
}
} else {
break;
}
left--;
right++;
}
}
auto no_space_end = std::remove(longest_str.begin(), longest_str.end(), '#');
longest_str.erase(no_space_end, longest_str.end());
return longest_str;
}
// 用左右下标记录最大回文子串,减少字符串拷贝
string longestPalindrome1(string s) {
string t = "#";
for (const auto& c : s) {
t += c;
t += "#";
}
int max_len = INT_MIN;
int left_max = 0;
int right_max = 0;
for (int i = 1; i < t.length() - 1; i++) {
int left = i;
int right = i;
while (left >= 0 && right < t.length()) {
if (t[left] == t[right]) {
if (max_len < right - left + 1) {
max_len = right - left + 1;
left_max = left;
right_max = right;
}
} else {
break;
}
left--;
right++;
}
}
string longest_str = t.substr(left_max, right_max - left_max + 1);
auto noSpaceEnd = std::remove(longest_str.begin(), longest_str.end(), '#');
longest_str.erase(noSpaceEnd, longest_str.end());
return longest_str;
}
// 空间复杂度o(1),不借助额外字符串修改
string longestPalindrome2(string s) {
int max_len = INT_MIN;
int left_max = 0;
int right_max = 0;
for (int i = 0; i < s.length(); i++) {
int left = i;
int right = i;
auto calc = [&]() {
while (left >= 0 && right < s.length()) {
if (s[left] == s[right]) {
if (right - left + 1 > max_len) {
max_len = right - left + 1;
left_max = left;
right_max = right;
}
} else {
break;
}
left--;
right++;
}
};
calc();
left = i;
right = i + 1;
calc();
}
string longest_str = s.substr(left_max, right_max - left_max + 1);
return longest_str;
}
// todo:进阶,Manacher
};
int main() {
Solution s;
cout << s.longestPalindrome("babad") << endl;
cout << s.longestPalindrome("a") << endl;
cout << s.longestPalindrome("") << endl;
cout << s.longestPalindrome("cbbd") << endl;
cout << "-----------------" << endl;
cout << s.longestPalindrome1("babad") << endl;
cout << s.longestPalindrome1("a") << endl;
cout << s.longestPalindrome1("") << endl;
cout << s.longestPalindrome1("cbbd") << endl;
cout << "-----------------" << endl;
cout << s.longestPalindrome2("babad") << endl;
cout << s.longestPalindrome2("a") << endl;
cout << s.longestPalindrome2("") << endl;
cout << s.longestPalindrome2("cbbd") << endl;
return 0;
}