125. Valid Palindrome

来源:互联网 发布:关闭手机数据连接 编辑:程序博客网 时间:2024/06/10 08:41
class Solution {public:    bool isPalindrome(string s)     {        int ls = s.size();                //s为空时显然是回文        //s只有1个字符时,不管该字符是否为字母或数字,s依然是回文        if(ls <= 1)            return true;                //s1用于存储s中的字母和数字,同时把大写字母转化为小写,便于比较        string s1;        for(int i = 0; i < ls; i++)        {            char c = s.at(i);            if(c >= 'a' && c <= 'z')                s1.push_back(c);            else if(c >= 'A' && c <= 'Z')                s1.push_back(c + 32);            else if(c >= '0' && c <= '9')                s1.push_back(c);        }                int ls1 = s1.size();                //与上面同理        if(ls1 <= 1)            return true;                //只要第i个与倒数第i个不同,就一定不是回文        for(int i = 0; i < ls1; i++)        {            int a = ls1 - i - 1;            if(s1.at(i) != s1.at(a))                return false;        }                return true;    }};

原创粉丝点击