add: #5
This commit is contained in:
parent
81cc1e1469
commit
3056c6223d
|
|
@ -10,3 +10,4 @@ ADD_EXECUTABLE(LeetCodeDaily main.cpp)
|
|||
|
||||
ADD_SUBDIRECTORY(2112)
|
||||
ADD_SUBDIRECTORY(2201)
|
||||
ADD_SUBDIRECTORY(more)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
|
||||
PROJECT(more)
|
||||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(more 0005.cpp)
|
||||
Loading…
Reference in New Issue