This commit is contained in:
Lam Haoyin 2022-01-21 12:59:10 +08:00
parent 81cc1e1469
commit 3056c6223d
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
3 changed files with 43 additions and 0 deletions

View File

@ -10,3 +10,4 @@ ADD_EXECUTABLE(LeetCodeDaily main.cpp)
ADD_SUBDIRECTORY(2112) ADD_SUBDIRECTORY(2112)
ADD_SUBDIRECTORY(2201) ADD_SUBDIRECTORY(2201)
ADD_SUBDIRECTORY(more)

36
more/0005.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <string>
#include <iostream>
class Solution {
public:
static std::string longestPalindrome(const std::string& s) {
int n = s.length(), centre, len = 0;
for (int i = 0; i < n; ++i) {
int L = i, R = i;
while (L - 1 >= 0 && R + 1 < n && s[L - 1] == s[R + 1]) {
--L, ++R;
}
if (len < R - L + 1) {
centre = i;
len = R - L + 1;
}
}
for (int i = 1; i < n; ++i) {
if (s[i] != s[i - 1]) continue;
int L = i - 1, R = i;
while (L - 1 >= 0 && R + 1 < n && s[L - 1] == s[R + 1]) {
--L, ++R;
}
if (len < R - L + 1) {
centre = i;
len = R - L + 1;
}
}
return s.substr(centre - len / 2, len);
}
};
int main() {
std::cout << Solution::longestPalindrome("eabcb");
return 0;
}

6
more/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
PROJECT(more)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(more 0005.cpp)