Beat over 98% in an easy JAVA solution


  • 0
    F

    public class Solution {
    public boolean isPalindrome(int x) {

        if (x < 0) {
            return false;
        }
        int reverse = 0;
        
        int copy = x;
        
        while (copy > 0) {
            int digit = copy % 10;
            copy /= 10;
            
            if ((Integer.MAX_VALUE-digit)/10 < reverse)
                return false;
            reverse = reverse*10 + digit;
        }
        
        if (x == reverse)
            return true;
        return false;
    }
    

    }


Log in to reply
 

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