add: 220402 [cpp]

This commit is contained in:
Lam Haoyin 2022-04-02 13:01:40 +08:00
parent dcded93d5f
commit 74595e4ee6
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 25 additions and 1 deletions

24
cpp/2204/220402.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <string>
/**
* 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;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220401.cpp) ADD_EXECUTABLE(2204 220402.cpp)