forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_50.java
39 lines (32 loc) · 731 Bytes
/
_50.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
package com.fishercoder.solutions;
/**
* 50. Pow(x, n)
*
* Implement pow(x, n).
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
*/
public class _50 {
public static class Solution1 {
public double myPow(double x, int n) {
if (n == 0) {
return 1;
}
if (n == Integer.MIN_VALUE) {
++n;
n = -n;
x = 1 / x;
return x * x * myPow(x * x, n / 2);
}
if (n < 0) {
n = -n;
x = 1 / x;
}
return (n % 2 == 0) ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
}
}
}