LeetCode第七题–反转32位的整数
代码
public class IntegerReverse {
public int reverse(int x) {
int absX = 0;
if(x < 0){
absX = Math.abs(x);
}else {
absX = x;
}
int result = 0 ;
char[] xChar = String.valueOf(absX).toCharArray();
for (int i = 0 ; i < (xChar.length)/2 ; i++) {
char temp = 0 ;
temp = xChar[i];
xChar[i] = xChar[xChar.length-1-i];
xChar[xChar.length-1-i] = temp;
}
try{
result = Integer.parseInt(String.valueOf(xChar));
}catch (Exception e){
return 0;
}
if (x < 0 ){
x = -1 * result;
return x;
}else {
x = result;
return x;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
参考资料
·1. https://github.com/zhujunpengguizhou/Algorithms