diff --git a/CMakeLists.txt b/CMakeLists.txt index 533e154..d490128 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,3 +10,4 @@ ADD_EXECUTABLE(LeetCodeDaily main.cpp) ADD_SUBDIRECTORY(2112) ADD_SUBDIRECTORY(2201) +ADD_SUBDIRECTORY(more) diff --git a/more/0005.cpp b/more/0005.cpp new file mode 100644 index 0000000..f18b7a2 --- /dev/null +++ b/more/0005.cpp @@ -0,0 +1,36 @@ +#include +#include + +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; +} diff --git a/more/CMakeLists.txt b/more/CMakeLists.txt new file mode 100644 index 0000000..3ab0542 --- /dev/null +++ b/more/CMakeLists.txt @@ -0,0 +1,6 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.21) +PROJECT(more) + +SET(CMAKE_CXX_STANDARD 23) + +ADD_EXECUTABLE(more 0005.cpp)