Leetcode 详解(valid plindrome)

时间:2021-09-12 21:36:31

Question: 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.
Example Questions Candidate Might Ask:
Q: What about an empty string? Is it a valid palindrome?
A: For the purpose of this problem, we define empty string as valid palindrome.
Solution:
O(n) runtime, O(1) space:
The idea is simple, have two pointers – one at the head while the other one at the tail.
Move them towards each other until they meet while skipping non-alphanumeric
characters.
Consider the case where given string contains only non-alphanumeric characters. This is
a valid palindrome because the empty string is also a valid palindrome.        

public boolean isPalindrome(String s) {

  int i = 0, j = s.length() - 1;

  while (i < j) {

    while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++;               //Character.asLetterOrDigit()  用来判断是不是字母或者数字
    while (i < j && !Character.isLetterOrDigit(s.charAt(j))) j--;
    if (Character.toLowerCase(s.charAt(i))!= Character.toLowerCase(s.charAt(j))) {    
      return false;

    }

    i++; j--;
  }
  return true;
}

注:这道题类似于用了两个指针在数组两头,同时,while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++ 用的很巧妙,避免了很多判断。