Http://7371901.blog.51cto.com/user_index.php?action=addblog_new
Http://fisherlei.blogspot.com/2012/12/leetcode-reverse-integer.html
Public class solution { public int reverse (int x) { // Solution a // return reverse_mod (x); // solution b return reverse_string (x); } /////////////////// // solution a: use % // private int reverse_mod (int x ) { long toReturn = 0; while (x != 0) { &nbSp; int lastdigit = x % 10 ; toreturn = toreturn * 10 + lastDigit; x = x / 10; if (Toreturn > integer.max_value | | toreturn < integer.min_value) return 0; // Overflow } return (int) toreturn; } ////////////////// / / solutioN b: use string // private int reverse_ String (int x) { // int to string // reverse // String to Long // Long to Int boolean negative = x < 0; string str = string.valueof (x); if (negative) str = str.substring (1, str.length ()); string reversedstr = new stringbuilder (str ). Reverse (). toString (); long thelong = long.parselong (REVERSEDSTR); if (Thelong > integer.max_value) return 0; // overflow int toreturn = thelong.intvalue (); if (negative) toreturn = -toreturn; return toreturn; } }
[Leetcode]1 Reverse Integer