-
Notifications
You must be signed in to change notification settings - Fork 0
/
029.cpp
35 lines (35 loc) · 906 Bytes
/
029.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
int divide(int divid, int divis) {
long long dividend=divid;
long long divisor=divis;
bool neg=false;
if(!divisor || (dividend == INT_MIN && divisor == -1)) return INT_MAX;
if(dividend==0) return 0;
if(dividend<0){
dividend=-dividend;
if(divisor<0){
neg=false;
divisor=-divisor;
}else{
neg=true;
}
}else if(divisor<0){
neg=true;
divisor=-divisor;
}
int ans=0;
int k=divisor;
while(divisor<=dividend){
long long temp=divisor,m=1;
while (dividend>=(temp<<1)) {
temp <<= 1;
m<<= 1;
}
dividend -= temp;
ans += m;
}
if(neg) return -ans;
return ans;
}
};