1
+ package easy ;
2
+ /**7. Reverse Integer QuestionEditorial Solution My Submissions
3
+ Total Accepted: 155938
4
+ Total Submissions: 655150
5
+ Difficulty: Easy
6
+ Reverse digits of an integer.
7
+
8
+ Example1: x = 123, return 321
9
+ Example2: x = -123, return -321*/
10
+ public class ReverseInteger {
11
+ //lastly, after making it AC'ed on my own, I turned to Discuss, and find this very short solution,
12
+ //it turns out we don't need to do any handling for the sign
13
+ public int reverse_short_version (int x ){
14
+ long rev =0 ;
15
+ while (x !=0 ){
16
+ rev =rev *10 +x %10 ;
17
+ x /=10 ;
18
+ if (rev >Integer .MAX_VALUE ||rev <Integer .MIN_VALUE )return 0 ;
19
+ }
20
+ return (int )rev ;
21
+ }
22
+
23
+ //made it AC'ed on my own, cheers!
24
+ public int reverse (int x ) {
25
+ if (x ==0 )return 0 ;
26
+ //save the first bit if it's a negative sign
27
+ StringBuilder sb =new StringBuilder ();
28
+ sb .append (x );
29
+ boolean negative =sb .toString ().charAt (0 ) =='-' ?true :false ;
30
+ //use modulor and division and use long as the result type to avoid overflow
31
+ long longX = (long )x ;
32
+ if (negative ){
33
+ //get rid of the first '-' bit
34
+ String withoutNegativeSign =sb .substring (1 ).toString ();
35
+ longX =Long .parseLong (withoutNegativeSign );
36
+ }
37
+ sb .setLength (0 );
38
+ long result =0 ;
39
+ if (negative ){
40
+ sb .append ('-' );
41
+ }
42
+ while (longX !=0 ){
43
+ sb .append (longX %10 );
44
+ longX /=10 ;
45
+ }
46
+ result =Long .parseLong (sb .toString ());
47
+ System .out .println (result );//it's right here, but after converting it into an int, it overflowed to become a wrong number, how to handle this?
48
+ //it turns out depending on the question requirement, on this OJ, it's expecting 0, if it's beyond the range of (Integer.MIN_VALUE, Integer.MAX_VALUE)
49
+ return (result >Integer .MAX_VALUE ||result <Integer .MIN_VALUE ) ?0 : (int )result ;
50
+ }
51
+
52
+ public static void main (String ...strings ){
53
+ ReverseInteger test =new ReverseInteger ();
54
+ //when the input is 1534236469, it's expecting 0 as the correct answer, this is due to its reversed number is greater than Integer.MAX_VALUE, thus return 0
55
+ System .out .println (1534236469 >Integer .MAX_VALUE );
56
+ System .out .println ("1534236469\n " +Integer .MAX_VALUE );
57
+ // System.out.println(test.reverse(-2147483648));
58
+ }
59
+
60
+ // this is not going to work when the input number's reverse version is greater than
61
+ // Integer.MAX_VALUE, it'll throw NumberFormatException as Java cannot handle it, overflowed.
62
+ public int reverse_overflowed (int x ) {
63
+ //save the first bit if it's a negative sign
64
+ StringBuilder sb =new StringBuilder ();
65
+ sb .append (x );
66
+ boolean negative =sb .toString ().charAt (0 ) =='-' ?true :false ;
67
+ char []bits =sb .toString ().toCharArray ();
68
+ sb .setLength (0 );
69
+ if (negative ){
70
+ sb .append ('-' );
71
+ //until i > 0
72
+ for (int i =bits .length -1 ;i >0 ;i --){
73
+ sb .append (bits [i ]);
74
+ }
75
+ }else {
76
+ //until i >= 0
77
+ for (int i =bits .length -1 ;i >=0 ;i --){
78
+ sb .append (bits [i ]);
79
+ }
80
+ }
81
+ return Integer .parseInt (sb .toString ());
82
+ }
83
+
84
+ }