69. x 的平方根
二分法
50. Pow(x, n)
快速幂方法,分治法。
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n == 0:
return 1
if n == 1:
return x
if n == -1:
return 1/x
half = self.myPow(x, n // 2)
if n % 2 == 0:
result = half * half
else:
result = half * half * x
return result
372. 超级次方
class Solution(object):
def mypow(self,x,n):
if n == 0:
return 1
if n == 1:
return x
if n == -1:
return 1/x
half = self.mypow(x, n // 2)
if n % 2 == 0:
result = half * half
else:
result = half * half * x
return result
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
if not b:
return 1
last = b.pop()
part1 = self.mypow(a, last)
part2 = self.mypow(self.superPow(a, b), 10)
return part1 * part2 % 1337
166. 分数到小数
166. 分数到小数https://leetcode-cn.com/problems/fraction-to-recurring-decimal/