The original question is as follows:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return-321
Click to show spoilers.
Has a thought about this ?
Here is some good questions to ask before coding. Bonus points for if you have already thought through this!
If the last digit are 0, what should the output being? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer and then the reverse of 1000000003 overflows. How should handle such cases?
For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.
My implementation code is as follows:
public class Solution {public int reverse (int x) {String s = string.valueof (x); if (S.equals ("0")) { return 0;} int length = S.length (); StringBuilder sb;if (S.startswith ("-")) {s = s.substring (1, length); sb = new StringBuilder (s); Sb.reverse (); while ( Sb.charat (0) = = ' 0 ') {sb.deletecharat (0);} Sb.insert (0, "-");} ELSE{SB = new StringBuilder (s); Sb.reverse (); while (Sb.charat (0) = = ' 0 ') {sb.deletecharat (0);}} if (Long.parselong (sb.tostring ()) >integer.max_value| | Long.parselong (Sb.tostring ()) <integer.min_value) {return 0;} Return Integer.parseint (Sb.tostring ());} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(leetcode) Inverse integer