class Solution {
public:
int divide(int dividend, int divisor) {
if(divisor==0||dividend==INT_MIN&&divisor==-1)
return INT_MAX;
long long a=labs(dividend);
long long b=labs(divisor);
int sign=(dividend>>31)^(divisor>>31)?-1:1;
int res=0;
while(a>=b)
{
long long c=b;
int i=1;//number that divisor is multiplied by
while(a>=c)
{
a-=c;
res+=i;
c<<=1;
i<<=1;
}
}
return sign==1?res:-res;
}
};
leetcode 29: Divide Two Integers
最新推荐文章于 2019-09-18 16:37:29 发布