-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (29 loc) · 1.02 KB
/
Solution.java
File metadata and controls
31 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package leetcode._29_;
class Solution {
public int divide(int dividend, int divisor) {
boolean positive = false;
if ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0)) {
positive = true;
}
long longDividend = Math.abs((long) dividend);
long longDivisor = Math.abs((long) divisor);
long result = 0;
while (longDividend >= longDivisor) { // 外层循环用来将剩余的继续算
long temp = longDivisor;
long tempResult = 1;
while (longDividend >= temp) { // 内层循环用来加快速度
longDividend -= temp;
result += tempResult;
tempResult <<= 1;
temp <<= 1;
}
}
if (positive && result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (!positive && result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return positive ? (int) result : (int) -result;
}
}