mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-09-23 15:34:20 +00:00
* add leetcode Pow(x, n) * Update 50.c fix comment * Update 50.c simplification * add lower_bound const. * updating DIRECTORY.md Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
40 lines
804 B
C
40 lines
804 B
C
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);
|
|
}
|