原题链接:https://leetcode.com/problems/valid-palindrome/description/
校验回文字符串,题目较为简单:
/**
* Created by clearbug on 2018/2/26.
*/
public class Solution {
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.isPalindrome("0P"));
System.out.println(s.isPalindrome("ab"));
System.out.println(s.isPalindrome("aba"));
System.out.println(s.isPalindrome("A man, a plan, a canal: Panama"));
System.out.println(s.isPalindrome("race a car"));
}
public boolean isPalindrome(String s) {
if (s == null) {
return false;
}
if (s.length() < 2) {
return true;
}
s = s.toLowerCase();
char[] sCharArr = s.toCharArray();
for (int i = 0, j = sCharArr.length - 1; i < j; i++, j--) {
while (i < j && !(sCharArr[i] >= 'a' && sCharArr[i] <= 'z') && !(sCharArr[i] >= '0' && sCharArr[i] <= '9')) {
i++;
}
while (i < j &&!(sCharArr[j] >= 'a' && sCharArr[j] <= 'z') && !(sCharArr[j] >= '0' && sCharArr[j] <= '9')) {
j--;
}
if (sCharArr[i] != sCharArr[j]) {
return false;
}
}
return true;
}
}