/**
* @Time : 19/03/2018 10:18
* @Author : lotus
* @File : ReverseInteger.java
* @Software: IntelliJ IDEA
* @Desc :
* @license : Copyright(C), Alex
* @Contact : alex.cs.monash@gmail.com
*/import java.util.Scanner;
/**
* Question 7: Reverse Integer
* <p>
* Given a 32-bit signed integer, reverse digits of an integer.
* Example 1:
* Input: 123
* Output: 321
* <p>
* Example 2:
* Input: -123
* Output: -321
* <p>
* Example 3:
* Input: 120
* Output: 21
* <p>
* Note:
* Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range.
* For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
*/publicclassReverseInteger {publicintreverse(int x) {
long result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x /= 10;
}
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
return0;
}
return (int) result;
}
publicstaticvoidmain(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
ReverseInteger reverseInteger = new ReverseInteger();
System.out.println(reverseInteger.reverse(input));
}
}