注意的几点
1.int 类型溢出问题
2.正负数问题
class Solution {
public:
int divide(int dividend, int divisor) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
long long ret=0,tmp=0,num=1;
bool sign=true;
if(dividend>0&&divisor<0||dividend<0&&divisor>0)
sign=false;
long long x=dividend,y=divisor;
x=abs(x);y=abs(y);
while(x>=y){
num=y;
tmp=1;
while(x>=num){
num=(num<<1);
tmp=tmp<<1;
}
num=num>>1;
tmp=tmp>>1;
x-=num;
ret+=tmp;
}
return sign?ret:-ret;
}
};