Problem :

https://leetcode.com/problems/powx-n/


My Solution :

class Solution:
def myPow(self, x: float, n: int) -> float:
if n < 0:
x = 1/x
n = -n
if n == 0:
return 1
if n == 1:
return x
if n % 2 == 0:
return self.myPow(x*x, n/2)
return x*self.myPow(x*x, (n-1)/2)


Comment :

이진 탐색처럼 곱하기 횟수를 O(logn)으로 줄여야 Time Limit Exceeded를 피할 수 있다.