` function count (a,b)
{
let cnt =0;
let yu =a;
while(yu>=b)
{
yu =yu - b;
cnt++;
}
return cnt;
}
var divide = function(a, b) {
let cnt=0;
if(a>0 && b>0)
{
if(a>= Math.pow(2, 31) || b>= Math.pow(2, 31)) return Math.pow(2, 31)-1;
cnt= count(a,b);
}
else if(a>=0 && b<0)
{
if(a>= Math.pow(2, 31) || b< Math.pow(-2, 31)) return Math.pow(2, 31)-1;
cnt = -count(a,-b);
}
else if(a<=0 && b>0)
{
if(a< Math.pow(-2, 31) || b>= Math.pow(2, 31)) return Math.pow(2, 31)-1;
if(a=== Math.pow(-2, 31) && b===1) return Math.pow(-2, 31);
cnt = -count(-a,b);
}
else
{
if(a< Math.pow(-2, 31) || b< Math.pow(-2, 31))
return Math.pow(2, 31)-1;
if(a=== Math.pow(-2, 31) && b===-1) return Math.pow(2, 31)-1;
cnt =count(-a,-b);
}
return cnt;
};`
- 需要注意的点:
- 认真审题,注意边界问题
- js里Math.pow(2, 31)是求幂