Java, simple and clear, using only int


  • 2
    C
    public class Solution {
    private static final int MAXDIV10 = Integer.MAX_VALUE / 10;
    private static final int MINDIV10 = Integer.MIN_VALUE / 10;
    public int reverse(int x) {
        int num = 0;
        while(x != 0){
        	int digit = x % 10;
        	if(num > MAXDIV10 || num < MINDIV10 || (num == MAXDIV10 && digit > 8) || (num == MINDIV10 && digit < -8))
        		return 0;
        	num = num * 10 + digit;
        	x /= 10;
        }
        return num;
    }
    

    }


Log in to reply
 

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