@@ -41,7 +41,7 @@ public static int div(int a, int b) {
41
41
int x = isNeg (a ) ? negNum (a ) : a ;
42
42
int y = isNeg (b ) ? negNum (b ) : b ;
43
43
int res = 0 ;
44
- for (int i = 31 ; i > negNum ( 1 ) ; i = minus (i , 1 )) {
44
+ for (int i = 31 ; i >= 0 ; i = minus (i , 1 )) {
45
45
if ((x >> i ) >= y ) {
46
46
res |= (1 << i );
47
47
x = minus (x , y << i );
@@ -51,17 +51,20 @@ public static int div(int a, int b) {
51
51
}
52
52
53
53
public static int divide (int dividend , int divisor ) {
54
- if (divisor == Integer .MIN_VALUE ) {
55
- return dividend == Integer .MIN_VALUE ? 1 : 0 ;
56
- }
57
- if (dividend == Integer .MIN_VALUE ) {
54
+ if (dividend == Integer .MIN_VALUE && divisor == Integer .MIN_VALUE ) {
55
+ return 1 ;
56
+ } else if (divisor == Integer .MIN_VALUE ) {
57
+ return 0 ;
58
+ } else if (dividend == Integer .MIN_VALUE ) {
58
59
if (divisor == negNum (1 )) {
59
60
return Integer .MAX_VALUE ;
61
+ } else {
62
+ int ans = div (add (dividend , 1 ), divisor );
63
+ return add (ans , div (minus (dividend , multi (ans , divisor )), divisor ));
60
64
}
61
- int res = div ( add ( dividend , 1 ), divisor );
62
- return add ( res , div (minus ( dividend , multi ( res , divisor )), divisor ) );
65
+ } else {
66
+ return div (dividend , divisor );
63
67
}
64
- return div (dividend , divisor );
65
68
}
66
69
67
70
}
0 commit comments