AC java solution, easy to understand.


  • 0
    B
     public boolean isPalindrome(String s) {
           if(s.length()==0) return true;
           s = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
           int left=0,right=0;
           if(s.length()%2==0){
        	   left=s.length()/2-1;
        	   right=s.length()/2;
        	   while(left>-1 && right<s.length() && s.charAt(left)==s.charAt(right)){
        		   left--;
        		   right++;
        	   }
           }
           if(s.length()%2==1){
        	   left=s.length()/2;
        	   right=s.length()/2;
        	   while(left>-1 && right<s.length() && s.charAt(left)==s.charAt(right)){
        		   left--;
        		   right++;
        	   }
           }
           return left==-1&&right==s.length()?true:false;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.