Easy and clean solution


  • 0
    R

    public class Solution {
    public int reverse(int x) {
    int sign = x < 0? -1:1;
    x = x < 0? -x : x;
    long result = 0;
    while (x > 0){
    long digit = x % 10;
    result = result * 10 + digit;
    x /= 10;
    }
    if (result > (long) Integer.MAX_VALUE){
    return 0;
    }
    return sign * (int) result;
    }
    }


Log in to reply
 

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