Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

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; }

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

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

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