125. Valid Palindrome LeetCode

来源:互联网 发布:我们来了网络几点更新 编辑:程序博客网 时间:2024/05/21 08:51

题意:判断一个字符串除掉空格和标点符号忽略大小写后是否是回文串。
题解:先把标点去掉,把字母统一变成小写,然后两头往中间判断回文。

class Solution {public:    bool check(char s)    {        if(isalpha(s) || isdigit(s)) return true;        else return false;    }    bool isPalindrome(string s) {        int n = s.length();        string ss;        for(int i = 0; i < n; i++)        if(check(s[i])) ss += s[i];        n = ss.length();         if(n == 0) return true;        for(int i = 0 ,j = n - 1; i < n - 1; i++,j--)        {            if(tolower(ss[i]) != tolower(ss[j])) return false;            else continue;        }        return true;    } };
0 0
原创粉丝点击