-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy path50.c
39 lines (31 loc) · 804 Bytes
/
50.c
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
double powPositive(double x, int n){
if (n == 1){
return x;
}
double val = powPositive(x, n / 2);
double result = val * val;
// if n is odd
if (n & 1 > 0){
result *= x;
}
return result;
}
// Divide and conquer.
// Runtime: O(log(n))
// Space: O(1)
double myPow(double x, int n){
if (n == 0){
return 1;
}
const int LOWER_BOUND = -2147483648;
// n is the minimum int, couldn't be converted in -n because maximum is 2147483647.
// this case we use (1 / pow(x, -(n + 1))) * n
if (n == LOWER_BOUND){
return 1 / (powPositive(x, -(n + 1)) * x);
}
// 1 / pow(x, -(n + 1))
if (n < 0){
return 1 / powPositive(x, -n);
}
return powPositive(x, n);
}