- Date : 2021.07.04(일)
- Time : 15분
- Implement pow(x, n), which calculates x raised to the power n (i.e., x^n).
- -100.0 < x < 100.0
- -2^31 <= n <= 2^31-1
- -10^4 <= xn <= 10^4
- Input: x = 2.00000, n = 10
- Output: 1024.00000
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n < 0:
x = 1 / x
n = -n
y = 1
while n > 1:
if n % 2 == 0:
x = x * x
n = n / 2
else:
y = x * y
x = x * x
n = (n - 1) / 2
return x * y
: 지수를 직접 표현해보는 식이였다. 먼저 지수가 0일 때의 모든 값은 1이기 때문에 경우의 수를 표현해주었다. 그리고 지수가 음수일 때는 분수로 표현되기 때문에 이 경우도 표현해주었다.