Leetcode Palindrome judge

 

Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.

Description: In this problem, we define the empty string as a valid palindromic sequence.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:

Input: "race a car"
Output: false

#include <iostream>
#include <string>
using namespace std;
/**
 * @brief:LeetCode---判断回文
 * @date:2020-3-15
 */


class solotion
{
public:
    /*判断是否为回文*/
    bool isPalindrom(string s) {
        if(s.empty()){
            return false ;
        }
     int start=0;
     int end=s.size()-1;
     while(start<end){
         if(!isAlphanumeric(s.at(start))){
             start++;
         }
         else if(!isAlphanumeric(s.at(end))){
             end--;
         }
         else if(::tolower(s.at(start))!=::tolower(s.at(end))){
             return false;
         }
         else{
         ++start;
         --end;
         }
     }
     return true;
    }
    /*判断是否为字符或者数字*/
    bool isAlphanumeric(char c);
public:
    bool res;
};
bool solotion::isAlphanumeric(char c)
{
    if((c>='A'&&c<'Z')||(c>'a'&&c<'z')||(c>'0'&&c<'9')){
        return true;
    }
    else
        return false;
}
/**
 * @brief main[主要是进行测试]
 * @param argc:NULL  命令行参数的个数
 * @param argv:NULL  主要是在数组中进行参数的存储
 * @return
 */
int main(int argc, char *argv[])
{
    solotion  sol;
    string  str="aq,q 1a";
    sol.res =sol.isPalindrom(str);
    cout<<sol.res<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

 

Published 102 original articles · won praise 26 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_42145502/article/details/104887542