[LeetCode][Python3] 50. Pow(x, n)
2019. 4. 24. 00:36 |
프로그래밍/LeetCode
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를 피할 수 있다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 765. Couples Holding Hands (0) | 2019.08.15 |
---|---|
[LeetCode][Python3] 854. K-Similar Strings (0) | 2019.08.15 |
[LeetCode][Python3] 5. Longest Palindromic Substring (0) | 2019.04.30 |
[LeetCode][Python3] 140. Word Break II (0) | 2019.04.25 |
[LeetCode][Python3] 212. Word Search II (0) | 2019.04.24 |
[LeetCode][Python3] 41. First Missing Positive (0) | 2019.04.23 |
[LeetCode][Python3] 152. Maximum Product Subarray (0) | 2019.04.22 |
[LeetCode][Python3] 124. Binary Tree Maximum Path Sum (0) | 2019.04.20 |
최근에 달린 댓글 최근에 달린 댓글