Concise Java


  • 0

    getNumericValue() method in Character class does the trick.

    public class Solution {
        public boolean isPalindrome(String s) {
            if (s.length() <= 1) return true; 
            
            int begin = 0, end = s.length() - 1; 
            
            while (begin < end) {
                while (begin < end && Character.getNumericValue(s.charAt(begin)) < 0 ) begin++; 
                while (begin < end && Character.getNumericValue(s.charAt(end)) < 0 ) end--; 
                if (Character.getNumericValue(s.charAt(begin)) != Character.getNumericValue(s.charAt(end))) return false; 
                else {
                    begin++; end--; 
                }
            }
            
            return true; 
        }
    }

Log in to reply
 

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