-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower.java
More file actions
32 lines (25 loc) · 691 Bytes
/
Copy pathPower.java
File metadata and controls
32 lines (25 loc) · 691 Bytes
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
package LeetcodePractice;
public class Power {
public static double myPow(double x, int n) {
if (n == 0) return 1;
long power = n;
if (n < 0) {
x = 1 / x;
power = -power;
}
double result = 1;
while (power > 0) {
if (power % 2 != 0) {
result *= x;
}
x *= x;
power /= 2;
}
return result;
}
public static void main(String[] args) {
System.out.println(myPow(2.00000, 10));
System.out.println(myPow(2.10000, 3));
System.out.println(myPow(2.00000, -2));
}
}