2ms Java using long no String


  • 0
    G
     public int reverse(int x) {
        if(x == 0 || x <= Integer.MIN_VALUE) return 0;
    	
    	long res = 0;
    	while(x != 0) {
    		res = res*10 + x%10;
    		x /= 10;
    	}
    	if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) return 0;
    	return (int)res;
    }

  • 0
    J

    how can you call this function with something less than Integer.MIN_VALUE?
    It would result in compile error "Integer number too large"

    if(x == 0 || x == Integer.MIN_VALUE) return 0;
    

    does the trick.

    Thanks


  • 0
    G

    I use res to store the result, it is a long type not int.


Log in to reply
 

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