My java solution(5ms)


  • 0
    R

    class Solution {
    public boolean isPalindrome(String s) {

        if(s.length() <= 1){
            return true;
        }
        
        int left = 0, right = s.length() - 1;
        while(left < right){
            
            int leftValue = convert(s.charAt(left));
            int rightValue = convert(s.charAt(right));
            if(leftValue == 0){
                left++;
            } else if(rightValue == 0){
                right--;
            } else {
                
                if(leftValue == rightValue){
                    left++;
                    right--;
                } else {
                    return false;
                }
                
            }
            
        }
        
        return true;
        
    }
    
    public int convert(char c){
        int value = (int)c;
        if((value >= 48 && value <= 57) 
           || (value >=65 && value <= 90)){
            return value;
        } else if((value >=97 && value <= 122)){
            return value - 32;
        } else {
            return 0;
        }
    }
    

    }


Log in to reply
 

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