29. 两数相除
题目链接:29. 两数相除
代码如下:
//参考链接:https://leetcode.cn/problems/divide-two-integers/solutions/954840/jian-dan-yi-dong-javac-pythonjs-liang-sh-ptbw
class Solution
{
public:
int divide(int dividend, int divisor)
{
if (dividend==INT_MIN&&divisor==-1) return INT_MAX;
int sign=(dividend>0)^(divisor>0)?-1:1;
if(dividend>0) dividend=-dividend;
if(divisor>0) divisor=-divisor;
int res=0;
while(dividend<=divisor)
{
int value=divisor;
int k=1;
while(value>=0xc0000000&÷nd<=value+value)
{
value+=value;
if(k>INT_MAX/2) return INT_MIN;
k+=k;
}
dividend-=value;
res+=k;
}
return sign==1?res:-res;
}
};