leetcode--Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
public class Solution {
public boolean isPalindrome(String s) {
boolean isPalindrome = true;
s = s.toLowerCase();
int length = s.length();
int left = 0, right= length - 1;
while(left <= right){
if((s.charAt(left) < 97 || s.charAt(left) > 122) && (s.charAt(left) < 48 || s.charAt(left) > 57))
++left;
else if((s.charAt(right) < 97 || s.charAt(right) > 122) && (s.charAt(right) < 48 || s.charAt(right) > 57))
--right;
else{
isPalindrome &= (s.charAt(left) == s.charAt(right));
if(!isPalindrome)
break;
++left;
--right;
}
}
return isPalindrome;
}
}

浙公网安备 33010602011771号