LeetCode 50.Pow(x、n)Pythonソリューション
Leetcode 50 Pow Python Solution
タイトル:実装 捕虜( バツ 、 n )。 。
計算を単純化するための2つの場所。1つは再帰計算ですが、x ^ 2計算を抽出するためです。
class Solution: def myPow(self, x, n): ''' :type x: float :type n: int :rtype: float ''' if(n==0): return 1.0 if(n<0): x = 1/x n = abs(n) x0 = x*x return self.myPow(x0, n//2) if(n%2 == 0) else x*self.myPow(x0, n//2)
効果