-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivide_Two_Integers_LeetCode_Important.cpp
More file actions
69 lines (42 loc) · 1.64 KB
/
Divide_Two_Integers_LeetCode_Important.cpp
File metadata and controls
69 lines (42 loc) · 1.64 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.
Note:
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, assume that your function returns 231 − 1 when the division result overflows.
*/
//Approach-1 Using Logarithmic Functions and Properties:
class Solution {
public:
int divide(int dividend, int divisor) {
if(dividend==0) {
return 0;
}
double t1=log(fabs(dividend));
double t2=log(fabs(divisor));
long long result=double(exp(t1-t2));
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0)) {
result*=-1;
}
return result>INT_MAX? INT_MAX:result;
}
};
//Approach-2 Using Bit Manipulation:
class Solution {
public:
int divide(int dividend, int divisor) {
long long n=dividend, m=divisor;
if(n <= INT_MIN && m == -1) return INT_MAX;
int sign = (n < 0) ^ (m < 0) ? -1: 1;
n=abs(n);
m=abs(m);
long long q=0, temp=0;
for(int i=31; i>=0; i--){
if(temp + (m << i) <= n){
temp += m << i;
q += 1 << i;
}
}
return sign * q;
}
};