Leetcode Note: Valid Palindrome
I. Description
Ii. problem-solving skills
The concept of palindrome originated from a class of numbers in mathematics that have the following characteristics:
Set n to any natural number. If you set the numbers of nReverse ArrangementIf the obtained natural number n1 is equal to n, n is called the return number. For example, if n = 1234321, n is called the number of replies. If n = 1234567, n is not the number of replies.
Similarly, you can define the number of replies in English or Chinese. The concept is similar to the above. This question is to check whether the string is the number of replies. Different from the digital text-back, the English sentence/phrase given in the question contains spaces and punctuation marks. Therefore, you must add another layer of judgment during algorithm design. The following functions may be used:
Isalpha () // If the parameter is a letter, a non-zero number is returned; otherwise, 0 isalnum () // If the parameter is a letter or number, a non-zero number is returned; otherwise, 0 isdigit () is returned. // If the parameter is a number (0-9), a non-zero number is returned. Otherwise, 0 is returned.
If the case sensitivity of strings is not required, you can add the following functions:
Transform (string. begin (), string. end (), string. begin (), toupper); // converts the content in the string to the uppercase letter transform (string. begin (), string. end (), string. begin (), tolower); // converts the content in the string to lowercase letters.
The description of the question. The empty string can also be considered as a return text. In addition, this question does not have complex logic problems and boundary conditions.
Iii. Sample Code
#include
#include
using std::string;class Solution{public: bool validPalindrome(string s) { if (s == ) return true; auto index_start = s.begin(), index_end = prev(s.end()); while (index_start < index_end) { if (!isalpha(*index_start)) index_start++; else if (!isalpha(*index_end)) index_end--; else if (*index_start == *index_end) { index_start++; index_end--; } else return false; } return true; }};
4. Example results
Enter the echo string:
Enter a non-return string: