Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).
Example
Given x = 123
, return 321
Given x = -123
, return -321
样例
给定 x = 123
,返回 321
给定 x = -123
,返回 -321
<pre name="code" class="java">public class Solution {
/**
* @param n the integer to be reversed
* @return the reversed integer
*/
public int reverseInteger(int n) {
long m = new Integer(0).longValue();
while (n != 0) {
m = m * 10 + n % 10;
n = n / 10;
}
if (m > Integer.MAX_VALUE) {return 0; }
else {return new Long(m).intValue(); }
}
}