Any improvements in running time?

  • -2

    public class ReverseInteger {

    public static int reverse(int x) {
    	long xl = 0;
    	int i = 0;
    	while (x != 0) {
    		// if xl=0 and x%10=0, nothing changes
    		xl = xl * 10 + x % 10;
    		x /= 10;
    	if (xl > Integer.MAX_VALUE || xl < Integer.MIN_VALUE)
    		return 0;
    	return (int) xl;


    What else should I do to reduce the running time? Still last 2ms

  • -1

    Maybe you can also judge whether xl is overflow in the while loop.

  • 0

    Maybe not, if I put the judgement in the loop, it would be executed in every time, while only happens truly in the last iteration.
    Now, it need only to be checked once.

  • 0

    I think the fastest submission now is 2ms right? I think this is optimized enough.

  • 0
    This post is deleted!

  • 0

    Thanks for ur attention. While I think there's misleading in your reply. Since the long type in Java occupies 64bits. So it won't overflow before x equals zero

Log in to reply

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