【leetcode】Valid Palindrome
来源:互联网 发布:未央歌 鹿桥 知乎 编辑:程序博客网 时间:2024/04/28 00:41
class Solution {public: bool check1(char ch)//当ch在'a'-'z'之间时,返回true { if(ch>='a'&&ch<='z') return true; return false; } bool check2(char ch)//当ch在'A'-'Z'之间时,返回true { if(ch>='A'&&ch<='Z') return true; return false; } bool check3(char ch)//当ch在'0'-'9'之间时,返回true { if(ch>='0'&&ch<='9') return true; return false; } bool isPalindrome(string s) { // Note: The Solution object is instantiated only once and is reused by each test case. if(s.size()==0||s.size()==1) return true; int sta=0,end=s.size()-1; while(sta<end) { while(check1(s[sta])==false&&check2(s[sta])==false&&check3(s[sta])==false) { sta++; if(sta>=s.length())//溢出 break; } while(check1(s[end])==false&&check2(s[end])==false&&check3(s[end])==false) { end--; if(end<0)//溢出 break; } if(check2(s[sta]))//大写转小写 s[sta]=s[sta]-'A'+'a'; if(check2(s[end])) s[end]=s[end]-'A'+'a'; if(sta<end&&s[sta]!=s[end]) return false; sta++; end--; } return true; }};