Use long to avoid overflow - Java


  • 0
    A
    public boolean isPalindrome(int x) {
           if(x<0){
                return false;
            }
    
            long temp = x,reverse=0;
            while(temp!=0){
                reverse = 10*reverse+temp%10;
                temp = temp/10;
            }
            return reverse==x;
        }

Log in to reply
 

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