50.x的n次幂

leetcode

递归
时间复杂度 O(log n)
空间复杂度O(log n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
func myPow(x float64, n int) float64 {

var helper func(float64, int) float64
helper = func(x float64, n int) float64 {
if n == 0 {
return 1
}

temp := helper(x, n / 2)

if n % 2 == 0 {
temp = temp * temp
} else {
temp = temp * temp * x
}

return temp
}

if n < 0 {
return 1 / helper(x, n)
}

return helper(x, n)
}

50.x的n次幂
https://blog.jerrylee.me/2021/09/2cc51ba2510e.html
作者
Jerry Lee
发布于
2021年9月20日
许可协议