# LeetCode 7：Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output:  321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

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.

package com.maoxiaomeng;

/**
* @author lihui
* @date 2018/3/25 22:31
*/
public class Solution {

public int reverse(int x) {

if (x == 0) {
return x;
}

while (x % 10 == 0 && x != 0) {
x = x / 10;
}

String s = x + "";
if (s.charAt(0) == '-') {
s = '-' + new StringBuffer(s.substring(1, s.length())).reverse().toString();
} else {
s = new StringBuffer(s).reverse().toString();
}

long temp = Long.valueOf(s);
if (temp > Integer.MAX_VALUE || temp < Integer.MIN_VALUE) {
temp = 0;
}
return (int)temp;
}
}

package com.maoxiaomeng;

/**
* @author lihui
* @date 2018/3/25 22:31
*/
public class Solution {

public int reverse(int x) {

long temp = 0;
while (x != 0) {
temp = temp * 10 + x % 10;
x = x / 10;
}

if (temp > Integer.MAX_VALUE || temp < Integer.MIN_VALUE) {
temp = 0;
}
return (int)temp;
}
}