From 74595e4ee675383cbd8c0aa4b1990dd2dfdba7c2 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sat, 2 Apr 2022 13:01:40 +0800 Subject: [PATCH] add: 220402 [cpp] --- cpp/2204/220402.cpp | 24 ++++++++++++++++++++++++ cpp/2204/CMakeLists.txt | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cpp/2204/220402.cpp diff --git a/cpp/2204/220402.cpp b/cpp/2204/220402.cpp new file mode 100644 index 0000000..dc00933 --- /dev/null +++ b/cpp/2204/220402.cpp @@ -0,0 +1,24 @@ +#include + +/** + * 680. Valid Palindrome II + * Given a string s, return true if the s can be palindrome after deleting at most one character from it. + */ + +class Solution { +private: + static bool isPalindrome(const std::string& basicString, int i, int j) { + for (; i < j; ++i, --j) + if (basicString[i] != basicString[j]) + return false; + return true; + } +public: + static bool validPalindrome(const std::string& s) { + for (int i = 0, j = s.size() - 1; i < j; ++i, --j) + if (s[i] != s[j]) + return isPalindrome(s, i + 1, j) || isPalindrome(s, i, j - 1); + return true; + } +}; + diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 67a70c6..810b8e8 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220401.cpp) +ADD_EXECUTABLE(2204 220402.cpp)