Solution in Java


  • 0
    V

    class Solution {
    public int reverse(int x) {

        boolean negativeFlag = false;
        if (x < 0)
        {
            negativeFlag = true;
            x = -x ;
        }
      
        int prev_rev_num = 0, rev_num = 0;
        while (x != 0)
        {
            int curr_digit = x%10;
      
            rev_num = (rev_num*10) + curr_digit;
      
            
            if ((rev_num - curr_digit)/10 != prev_rev_num)
            {
                System.out.println("WARNING OVERFLOWED!!!");
                return 0;
            }
      
            prev_rev_num = rev_num;
            x = x/10;
        }
      
        return (negativeFlag == true)? -rev_num : rev_num;
    }
    }

Log in to reply
 

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