class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n < 0:
return 1 / self.myPow(x, -n)
res = 1
for _ in range(n):
res *= x
return res
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n == 1:
return x
if n < 0:
return 1 / self.myPow(x, -n)
return self.myPow(x, n // 2) * self.myPow(x, n - n // 2)
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n == 1:
return x
if n < 0:
return 1 / self.myPow(x, -n)
return self.myPow(x * x, n // 2) if n % 2 == 0 else x * self.myPow(x, n - 1)
class Solution {
double myPow(double x, long n) {
if (n < 0) return 1 / myPow(x, -n);
if (n == 0) return 1;
if (n == 1) return x;
if (n == 2) return x * x;
return myPow(myPow(x, n / 2), 2) * (n % 2 ? x : 1);
}
public:
double myPow(double x, int n) {
return myPow(x, (long)n);
}
};
class Solution:
def myPow(self, x: float, n: int) -> float:
if n < 0:
return 1 / self.myPow(x, -n)
res = 1
while n:
if n & 1 == 1:
res *= x
x *= x
n >>= 1
return res