Java Solution for Reverse Integer(complementation)


  • 0
    J

    1.Get length of this Integer.
    2.complementation and division

    class Solution {
    public int reverse(int x) {
    if (x == 0) return x;
    long result = 0;
    int xLen = String.valueOf(x).length();
    for (int i = 0; i < xLen && x != 0; i++) {
    result *= 10;
    result += x % 10;
    x /= 10;
    }
    if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) result = 0;
    return (int)result;
    }
    }


Log in to reply
 

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