Accepted Java Solution Without direct variable declaration


  • 0
    T
    public boolean isPalindrome(int x) {
        if (x < 0) return false;
        if (x < 10) return true;
        
        if ((int) (x / Math.pow(10, Math.floor(Math.log10(x)))) == x % 10) {
            if (x < 100) return true;
                        
            if (Math.floor(Math.log10(x)) - Math.floor(Math.log10(x % Math.pow(10, (int) (Math.log10(x))))) > 1) {
            	if (Math.floor(x % Math.pow(10, (int) (Math.log10(x))) / 10 % Math.pow(10, Math.floor(Math.log10(x)) - Math.floor(Math.log10(x % Math.pow(10, (int) (Math.log10(x))))) -1)) != 0) {
                	return false;
                } else {
                	x = (int) Math.floor(x % Math.pow(10, (int) (Math.log10(x))) / Math.pow(10, Math.floor(Math.log10(x)) - Math.floor(Math.log10(x % Math.pow(10, (int) (Math.log10(x)))))));
                }
            } else {     
            	x %= Math.pow(10, (int) (Math.log10(x)));            
            	x /= 10;
            }
    
            return isPalindrome(x);
        } else return false;
    }

Log in to reply
 

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