-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path005longestPalindromicSubstring.cpp
More file actions
78 lines (70 loc) · 2.24 KB
/
005longestPalindromicSubstring.cpp
File metadata and controls
78 lines (70 loc) · 2.24 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
//
// Created by Pasco on 16/4/4.
//
#include "catch.hpp"
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
/*
* 字符串长度为0和1的两种特殊情况*/
if (s.length() == 0) {
return "";
}
if (s.length() == 1) {
return s;
}
string res;
int j = 0;
int k = 0;
int startPosition = 0;
int maxLength = 0;
for (int i = 0; i < s.length();) {
/*
* 如果剩余的字符串长度还没有已知最大长度回文字符长度的一半长
* 那么就没必要往下找了,已知的那个就是最长了
* */
if(s.length()-i < maxLength/2){
break;
}
k = i;
j = i;
/*
* 如果有连续重复出现的字符
* 那么k直接跳到重复字符结束的位置
* 因为连续重复的字符肯定是回文
* 所以我们要看看重复字符的两侧位置
* sample: abccccccccbe
* 我们要从第一个c的前面和最后一个c后面开始对比
* j的位置是第一个c的位置
* 所以我们要把k挪到最后一个c的位置
* 然后对比s[k++] == s[j--]是否成立
* 直到找到不成立的地方
* j就是初始位置,k就是回文结束位置
* 长度即为 k-j+1
* */
while (k<s.length()-1 && s[k]==s[k+1]){
k++;
}
i = k+1;
while (k<s.length()-1 && j>0 && s[j-1]==s[k+1]) {
k++;
j--;
}
int newStringLength = k-j+1;
if(newStringLength > maxLength) {
startPosition = j;
maxLength = newStringLength;
}
}
res = s.substr(startPosition,maxLength);
return res;
}
};
TEST_CASE("case 1 for longestPalindrome") {
Solution solution;
string fooString = "ajdfabbcbbashfja";
string expectedOutput = "abbcbba";
string actualOutput = solution.longestPalindrome(fooString);
REQUIRE(expectedOutput == actualOutput);
}