var divide = function (dividend, divisor) {
//dividend 为被除数 divisor 为除数
//记录符号
let flag1 = dividend >= 0
let flag2 = divisor >= 0
//转换成正数
dividend = Math.abs(dividend)
divisor = Math.abs(divisor)
//对除数进行翻倍 存储待用
let count = 1
const arr = []
while (dividend >= divisor) {
arr.push([count, divisor])
divisor += divisor
count += count
}
//让被除数不断减去arr中小于它的除数,从arr中最大的值开始减
let sum = 0 //记录除数的个数
const len = arr.length
for (let i = len - 1; i >= 0 && dividend > 0; i--) {
let [count, divisor] = arr[i]
if (dividend >= divisor) {
dividend -= divisor
sum += count
}
}
return flag1 === flag2 ? Math.min(sum, 2147483648 - 1) : -Math.min(sum, 2147483648)
};