-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathPowXN.java
85 lines (77 loc) · 2.24 KB
/
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.leetcode.arrays.binarysearch;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Medium
* Link: https://leetcode.com/problems/powx-n/
* Description:
* Implement pow(x, n), which calculates x raised to the power n (x^n).
* <p>
* Example 1:
* Input: 2.00000, 10
* Output: 1024.00000
* <p>
* Example 2:
* Input: 2.10000, 3
* Output: 9.26100
* <p>
* Example 3:
* Input: 2.00000, -2
* Output: 0.25000
* Explanation: 2^-2 = 1/22 = 1/4 = 0.25
* <p>
* Note:
* -100.0 < x < 100.0
* n is a 32-bit signed integer, within the range [−231, 231 − 1]
*
* @author rampatra
* @since 2019-08-19
*/
public class PowXN {
/**
* In this approach we iterate n times and keep multiplying x with x.
* Runtime: <a href="https://leetcode.com/submissions/detail/253075786/">Time limit exceeded</a>.
*
* @param x
* @param n
* @return
*/
public static double myPowNaive(double x, int n) {
if (n == 0) {
return 1;
}
double res = x;
int absN = Math.abs(n);
for (int i = 1; i < absN; i++) {
res *= x;
}
return n < 0 ? 1 / res : res;
}
/**
* In this approach, we iterate log n times. We omit half of n each time. When n is odd, we store whatever product
* we have calculated so far in the final result.
* <p>
* Runtime: <a href="https://leetcode.com/submissions/detail/253276630/">1 ms</a>.
*
* @param x
* @param n
* @return
*/
public static double myPow(double x, int n) {
double res = 1;
long absN = Math.abs((long) n); // used a long so that `absN / 2` doesn't overflow
while (absN > 0) {
if (absN % 2 == 1) res *= x; // store whatever we have calculated so far in the final result
x *= x;
absN /= 2;
}
return n < 0 ? 1 / res : res;
}
public static void main(String[] args) {
assertEquals(1024.0, myPowNaive(2.0, 10));
assertEquals(0.25, myPowNaive(2.0, -2));
assertEquals(0.0, myPowNaive(0.00001, 2147483647));
assertEquals(1024.0, myPow(2.0, 10));
assertEquals(0.25, myPow(2.0, -2));
assertEquals(0.0, myPow(0.00001, 2147483647));
}
}