-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathPow.java
More file actions
54 lines (50 loc) · 1.26 KB
/
Pow.java
File metadata and controls
54 lines (50 loc) · 1.26 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
package algorithm.lc;
/**
* Implement pow(x, n)
*
*/
public class Pow {
// O(1) space, O(logn) time, if length of number is fixed, it is constant
// running time
public class Solution {
public double pow(double x, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if (n == Integer.MIN_VALUE) {
return Math.abs(x) == 1? 1 : 0;
}
boolean isPositive = n > 0;
n = Math.abs(n);
// right shift index each time
double res = 1;
double tmp = x;
while (n > 0) {
if (n % 2 == 1) {
res *= tmp;
}
tmp *= tmp;
n >>= 1;
}
return isPositive ? res : 1.0 / res;
}
}
// O(1) space, O(logn) time
public class Solution2 {
// recursive x^n = x^(n / 2) * x^(n / 2) * x^(n % 2)
public double pow(double x, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if (n == 0) {
return 1;
}
else if (n == Integer.MIN_VALUE) {
return Math.abs(x) == 1? 1 : 0;
}
boolean isPositive = n > 0;
n = Math.abs(n);
double res = pow(x, n / 2);
res *= res * (n % 2 == 0 ? 1 : x);
return isPositive ? res : 1.0 / res;
}
}
}