递增递减索引时候要时刻注意索引所在的界限。并且我写的代码具有逻辑冗余。
class Solution {
public:
bool isPalindrome(string s)
{
int endIndex=s.size()-1,startIndex=0;
if(endIndex==-1)
{
return true;
}
bool toContinue=true;
while(toContinue&&endIndex>startIndex)
{
while(!isalnum(s[endIndex])&&endIndex>startIndex)
{
endIndex--;
}
while(!isalnum(s[startIndex])&&startIndex<endIndex)
{
startIndex++;
}
if(tolower(s[startIndex])==tolower(s[endIndex]))
{
startIndex++;
endIndex--;
}
else
{
toContinue=false;
}
}
if(toContinue)
{
return true;
}
else
{
return false;
}
}
};