7. Reverse Integer
1 class Solution { 2 public int reverse(int x) { 3 long x2 = (long)x; 4 if(x == 0) return 0; 5 if(x2 < 0) { 6 x2 = -x2; 7 String str = "" + x2; 8 StringBuilder sb = new StringBuilder(str); 9 sb.reverse(); 10 long x1 = Long.parseLong(sb.toString()); 11 if(-x1 < Integer.MIN_VALUE) { 12 return 0; 13 }else { 14 return (int)-x1; 15 } 16 }else { 17 String str = "" + x; 18 StringBuilder sb = new StringBuilder(str); 19 sb.reverse(); 20 long x1 = Long.parseLong(sb.toString()); 21 if(x1 > Integer.MAX_VALUE) { 22 return 0; 23 }else { 24 return (int)x1; 25 } 26 } 27 28 } 29 }