Files
LeetCode-Go/leetcode/0050.Powx-n/50. Pow(x, n).go
2020-08-07 17:06:53 +08:00

21 lines
279 B
Go

package leetcode
// 时间复杂度 O(log n),空间复杂度 O(1)
func myPow(x float64, n int) float64 {
if n == 0 {
return 1
}
if n == 1 {
return x
}
if n < 0 {
n = -n
x = 1 / x
}
tmp := myPow(x, n/2)
if n%2 == 0 {
return tmp * tmp
}
return tmp * tmp * x
}