题目描述:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
输入: 123 输出: 321
输入: -123 输出: -321
输入: 120 输出: 21
代码实现:
public static int reverse(int x) { if (x >= 0) { String temp = x + ""; String str = new StringBuilder(temp).reverse().toString(); try{ int i = Integer.valueOf(str); return i; }catch(Exception e){ return 0; } } else{ String temp = (x + "").replace("-",""); String str = new StringBuilder(temp).reverse().toString(); try{ return 0-Integer.valueOf(str); }catch(Exception e){ return 0; } } }