public class Solution { public int reverse(int x) { char[] sArray = String.valueOf(x).toCharArray(); StringBuilder sb = new StringBuilder(); int flag=1; if (x == 0) {//处理x=0 sb.append(x); } else { int min = 0; if (x < 0) { min = 1; flag=-1; } boolean isStart = false;//处理类似x=123000或x=-123000情况 int len = sArray.length - 1; for (int i = len; i >= min; i--) { if (!isStart) { if (sArray[i] != '0') { isStart = true; sb.append(sArray[i]); } } else { sb.append(sArray[i]); } } } String str=sb.toString(); if (x >= 0) { if (Long.parseLong(str) >2147483647) { return 0; } else { return Integer.parseInt(str); } } else { if (Long.parseLong(str) >2147483648L) { return 0; } else { return Integer.parseInt(str)*flag; } } } }
原文:http://blog.csdn.net/wj512416359/article/details/42024425