lintcode-有效回文字符串

来源:互联网 发布:java期末考试题库 编辑:程序博客网 时间:2024/05/18 02:27

给定一个字符串,判断其是否为一个回文串。只包含字母和数字,忽略大小写

例如 "A man, a plan, a canal: Panama" 是一个回文。

     "race a car" 不是一个回文。

class Solution {public:       bool check(char ch){        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))            return false;        return true;        }         bool isPalindrome(string& s) {                if(s.empty())            return true;        if(s.size()==1)            return true;        int size=s.size();            int end=size-1;        int begin=0;                while(begin<end){            while(end>=0&&check(s[end])){                --end;            }            while(begin<size&&check(s[begin])){                ++begin;            }              if(tolower(s[end])!=tolower(s[begin]))                 return false;            else                --end,++begin;                     }        return true;    }};


0 0