解题思路:
双指针
时间复杂度:O(n)提交代码:
class Solution {
public boolean isPalindrome(String s) {
int p1=0,p2=s.length()-1;
while(p1<p2) {
while(!isChar(s.charAt(p1))&&p1<p2)
p1++;
while(!isChar(s.charAt(p2))&&p2>p1)
p2--;
if(p1>=p2) break;
if(reshape(s.charAt(p1))!=reshape(s.charAt(p2)))
return false;
p1++;p2--;
}
return true;
}
private boolean isChar(char c) {
if(c-'a'>=0&&c-'a'<26) return true;
if(c-'A'>=0&&c-'A'<26) return true;
if(c-'0'>=0&&c-'0'<=9) return true;
return false;
}
private char reshape(char c) {
char newChar='a';
if(c-'A'>=0&&c-'A'<26) {
newChar=(char)('a'+c-'A');
return newChar;
}
return c;
}
}
运行结果: