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. https://github.com/zhujunpengguizhou/Algorithms