首页 > 代码库 > 125. Valid Palindrome

125. 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.

思路:双指针夹逼,判断首位对应的字母是否相同。Character.isLetterOrDigit这个method好好用!Reverse Words in String就可以用这个method和stack读出来words,然后pop出来。

public class Solution {    public boolean isPalindrome(String s) {        int i=0,j=s.length()-1;        while(i<j)        {            while(i<j&&!Character.isLetterOrDigit(s.charAt(i)))            {                i++;            }            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;            }}

 

125. Valid Palindrome