Question

Formatted question description: https://leetcode.ca/all/7.html

7	Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321
Example 3:
	Input: 120
	Output: 21

[click to show spoilers.]

Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

Algorithm

One thing to pay attention to when flipping numbers is the overflow problem. After reading many online solutions, since the previous OJ did not test for overflow, many people’s solutions on the internet can pass OJ without dealing with the overflow problem.

Now OJ has updated the overflow test, so it still needs to be considered. Why is there an overflow problem? Since the value range of the int type is -2147483648~2147483647, if you want to flip the number within the range of 1000000009 to get 9000000001, the number after the flip exceeds the range. The initial idea of ​​the blogger is to use long data, the value range of which is -9223372036854775808~9223372036854775807, which is much larger than int so that there will be no overflow problem.

Code

Java

public class Reverse_Integer {

	public static void main(String[] args) {
		Reverse_Integer out = new Reverse_Integer();
		Solution s = out.new Solution();

		System.out.println(s.reverse(1234567899));


		System.out.println(1234567899 * 10); // output: -539222898
	}

	/*

	from online:

		If overflow exists, the new result will not equal previous one.
		No flags needed. No hard code like 0xf7777777 needed.
	*/
	public class Solution {
		public int reverse(int x) {
			int result = 0;

			while (x != 0) {
				int tail = x % 10;
				int newResult = result * 10 + tail;

				// If overflow exists, the new result will not equal previous one.
				/*
					result = 998765432

					newResult = 1397719729 // here the program will do a cast, meaning overflow

				 */
				if ((newResult - tail) / 10 != result) {
					return 0;
				}

				result = newResult;

				x = x / 10;
			}

			return result;
		}
	}


	public class Solution_longType {
	    public int reverse(int x) {

	        int flag = 1;

	        if (x < 0) {
	            x *= -1;
	            flag = -1;
	        }

	        long result = 0; // initialize as long type

	        while(x > 0) {
	            int least = x % 10;
	            result = result * 10 + least;
	            x /= 10;
	        }

	        // check overflow
	        if(result > Integer.MAX_VALUE || result * flag < Integer.MIN_VALUE) {
	            return 0;
	        }

	        return (int) (flag * result);
	    }
	}
}

All Problems

All Solutions