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;
}
};
【leetcode】Valid Palindrome
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.