forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowXN.java
33 lines (29 loc) · 769 Bytes
/
PowXN.java
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
package binary_search;
/**
* Created by gouthamvidyapradhan on 23/05/2017.
* <p>
* Implement pow(x, n).
* <p>
* Solution: Works with O(log n)
*/
public class PowXN {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(1 / new PowXN().myPow(2.00000, -2147483648));
}
public double myPow(double x, int n) {
if (n == 0) return 1D;
long N = n; //use long to avoid overflow.
return solve(n < 0 ? (1 / x) : x, N < 0 ? (N * -1) : N);
}
public double solve(double x, long n) {
if (n == 1) return x;
double val = solve(x, n / 2);
return val * val * ((n % 2) == 0 ? 1 : x);
}
}