aboutsummaryrefslogtreecommitdiff
path: root/works/solutions/leetcode/cpp/5.cpp
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-02-28 23:13:39 +0800
committerYuqian Yang <crupest@crupest.life>2025-02-28 23:13:39 +0800
commitdc1f0c4c0096013799416664894c5194dc7e1f52 (patch)
tree2f5d235f778cd720f4c39ec3e56b77ba6d99f375 /works/solutions/leetcode/cpp/5.cpp
parent7299d424d90b1effb6db69e3476ddd5af72eeba4 (diff)
downloadcrupest-dc1f0c4c0096013799416664894c5194dc7e1f52.tar.gz
crupest-dc1f0c4c0096013799416664894c5194dc7e1f52.tar.bz2
crupest-dc1f0c4c0096013799416664894c5194dc7e1f52.zip
chore(store): move everything to store.
Diffstat (limited to 'works/solutions/leetcode/cpp/5.cpp')
-rw-r--r--works/solutions/leetcode/cpp/5.cpp104
1 files changed, 0 insertions, 104 deletions
diff --git a/works/solutions/leetcode/cpp/5.cpp b/works/solutions/leetcode/cpp/5.cpp
deleted file mode 100644
index 6200c4c..0000000
--- a/works/solutions/leetcode/cpp/5.cpp
+++ /dev/null
@@ -1,104 +0,0 @@
-#include <string>
-
-using std::string;
-
-class Solution
-{
-public:
- void longestWithCenter(const string &s, int length, int index, int *longest_start, int *longest_length_ptr)
- {
- int &longest_length = *longest_length_ptr;
-
- int palindrome_length_odd = index * 2 + 1 <= longest_length || (length - index - 1) * 2 + 1 <= longest_length ? 0 : 1;
- int palindrome_length_even = (index + 1) * 2 <= longest_length || (length - index - 1) * 2 <= longest_length ? 0 : 1;
-
- while (palindrome_length_odd || palindrome_length_even)
- {
- if (palindrome_length_odd)
- {
- int start = index - palindrome_length_odd;
- int end = index + palindrome_length_odd;
- if (start < 0)
- {
- palindrome_length_odd = 0;
- }
- else if (end >= length)
- {
- palindrome_length_odd = 0;
- }
- else
- {
- if (s[start] == s[end])
- {
- int current_length = end - start + 1;
- if (current_length > longest_length)
- {
- *longest_start = start;
- longest_length = current_length;
- }
- palindrome_length_odd++;
- }
- else
- {
- palindrome_length_odd = 0;
- }
- }
- }
-
- if (palindrome_length_even)
- {
- int start = index - palindrome_length_even + 1;
- int end = index + palindrome_length_even;
- if (start < 0)
- {
- palindrome_length_even = 0;
- }
- else if (end >= length)
- {
- palindrome_length_even = 0;
- }
- else
- {
- if (s[start] == s[end])
- {
- int current_length = end - start + 1;
- if (current_length > longest_length)
- {
- *longest_start = start;
- longest_length = current_length;
- }
- palindrome_length_even++;
- }
- else
- {
- palindrome_length_even = 0;
- }
- }
- }
- }
- }
-
- string longestPalindrome(string s)
- {
- if (s.empty())
- return "";
-
- int longest_start = 0;
- int longest_length = 1;
- int length = s.size();
-
- for (int i = 0; i < length; i++)
- {
- longestWithCenter(s, length, i, &longest_start, &longest_length);
- }
-
- return s.substr(longest_start, longest_length);
- }
-};
-
-int main()
-{
- Solution s{};
- s.longestPalindrome("bb");
- return 0;
-}