1
1
package com .fishercoder .solutions ;
2
- /**7. Reverse Integer
3
- Reverse digits of an integer.
4
2
5
- Example1: x = 123, return 321
6
- Example2: x = -123, return -321*/
7
- public class _7 {
8
- public int reverse_short_version (int x ) {
9
- long rev = 0 ;
10
- while (x != 0 ) {
11
- rev = rev * 10 + x % 10 ;
12
- x /= 10 ;
13
- if (rev > Integer .MAX_VALUE || rev < Integer .MIN_VALUE ) {
14
- return 0 ;
15
- }
16
- }
17
- return (int ) rev ;
18
- }
3
+ /**
4
+ * 7. Reverse Integer
5
+ *
6
+ * Reverse digits of an integer.
19
7
20
- public int reverse (int x ) {
21
- if (x == 0 ) {
22
- return 0 ;
23
- }
24
- //save the first bit if it's a negative sign
25
- StringBuilder sb = new StringBuilder ();
26
- sb .append (x );
27
- boolean negative = sb .toString ().charAt (0 ) == '-' ? true : false ;
28
- //use modulor and division and use long as the result type to avoid overflow
29
- long longX = (long ) x ;
30
- if (negative ) {
31
- //get rid of the first '-' bit
32
- String withoutNegativeSign = sb .substring (1 ).toString ();
33
- longX = Long .parseLong (withoutNegativeSign );
34
- }
35
- sb .setLength (0 );
36
- long result = 0 ;
37
- if (negative ) {
38
- sb .append ('-' );
39
- }
40
- while (longX != 0 ) {
41
- sb .append (longX % 10 );
42
- longX /= 10 ;
43
- }
44
- result = Long .parseLong (sb .toString ());
45
- 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?
46
- //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)
47
- return (result > Integer .MAX_VALUE || result < Integer .MIN_VALUE ) ? 0 : (int ) result ;
48
- }
8
+ Example1: x = 123, return 321
49
9
50
- public static void main (String ... strings ) {
51
- _7 test = new _7 ();
52
- //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
53
- System .out .println (1534236469 > Integer .MAX_VALUE );
54
- System .out .println ("1534236469\n " + Integer .MAX_VALUE );
55
- // System.out.println(test.reverse(-2147483648));
56
- }
10
+ Example2: x = -123, return -321
57
11
58
- // this is not going to work when the input number's reverse version is greater than
59
- // Integer.MAX_VALUE, it'll throw NumberFormatException as Java cannot handle it, overflowed.
60
- public int reverse_overflowed (int x ) {
61
- //save the first bit if it's a negative sign
62
- StringBuilder sb = new StringBuilder ();
63
- sb .append (x );
64
- boolean negative = sb .toString ().charAt (0 ) == '-' ? true : false ;
65
- char [] bits = sb .toString ().toCharArray ();
66
- sb .setLength (0 );
67
- if (negative ) {
68
- sb .append ('-' );
69
- //until i > 0
70
- for (int i = bits .length - 1 ; i > 0 ; i --) {
71
- sb .append (bits [i ]);
72
- }
73
- } else {
74
- //until i >= 0
75
- for (int i = bits .length - 1 ; i >= 0 ; i --) {
76
- sb .append (bits [i ]);
12
+ */
13
+ public class _7 {
14
+
15
+ public static class Solution1 {
16
+ public int reverse (int x ) {
17
+ long rev = 0 ;
18
+ while (x != 0 ) {
19
+ rev = rev * 10 + x % 10 ;
20
+ x /= 10 ;
21
+ if (rev > Integer .MAX_VALUE || rev < Integer .MIN_VALUE ) {
22
+ return 0 ;
23
+ }
77
24
}
25
+ return (int ) rev ;
78
26
}
79
- return Integer .parseInt (sb .toString ());
80
27
}
81
-
82
28
}
0 commit comments