-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
51 lines (49 loc) · 1.24 KB
/
solution.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @param {number} dividend
* @param {number} divisor
* @return {number}
*/
var divide = function (dividend, divisor) {
var symbol = 1;
if (dividend < 0 && divisor < 0) {
dividend = 0 - dividend;
divisor = 0 - divisor;
} else if (dividend < 0) {
symbol = -1;
dividend = 0 - dividend;
} else if (divisor < 0) {
symbol = -1;
divisor = 0 - divisor;
}
var sum = 0;
var preRatio = 1;
var ratio = 1;
var preDivisor = divisor;
var multiDivisor = divisor;
var rst = 0;
while (true) {
if ((sum + multiDivisor) > dividend) {
if (ratio === 1) {
break;
}
rst += preRatio;
sum += preDivisor;
preRatio = 1;
ratio = 1;
preDivisor = divisor;
multiDivisor = divisor;
} else if ((sum + multiDivisor) === dividend) {
rst += ratio;
break;
} else {
preRatio = ratio;
ratio += ratio;
preDivisor = multiDivisor;
multiDivisor += multiDivisor;
}
}
if (symbol > 0 && rst > 2147483647) {
return 2147483647;
}
return symbol > 0 ? rst : 0 - rst;
};