Shorter and Simpler Explanative Solution in java


  • 0
    R

    class Solution {
    public int reverse(int x) {
    int result = 0;
    while (x != 0) {
    int temp = result * 10 + x%10; //If x is negative, x%10 returns negative value
    if ((temp-x%10)/10 != result) { //It will not be equal when it crosses max_int, we cant store max, so 0
    return 0;
    }
    result = temp;
    x = x/10;
    }
    return result;
    }
    }


Log in to reply
 

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