-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0029_Divide_Two_Integers.cpp
More file actions
50 lines (43 loc) · 1.24 KB
/
Copy path0029_Divide_Two_Integers.cpp
File metadata and controls
50 lines (43 loc) · 1.24 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
long long output = 0, size = 1;
long long pos_dend, sor;
pos_dend = abs((long long)dividend);
sor = abs((long long)divisor);
// each iter, substract a number 2 times as last
// could use shift operation, save memory
while(pos_dend - sor >= 0){
output += size;
pos_dend -= sor;
sor <<= 1;
size <<= 1;
}
// substract the all rest of sors vector
while(size > 0){
if(pos_dend >= sor){
output += size;
pos_dend -= sor;
}
sor >>= 1;
size >>= 1;
}
// signed
if(!((dividend > 0) && (divisor > 0) || (dividend < 0) && (divisor < 0)))
output = -output;
if((output > 2147483647) || (output < -2147483648))
return 2147483647;
return output;
}
};
int main(){
Solution solve;
int dividend = -2147483648;
int divisor = -1;
cout << "Input: " << dividend << " " << divisor << endl;
cout << "Output: " << solve.divide(dividend, divisor) << endl;
return 0;
}