mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
21 lines
279 B
Go
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
|
|
}
|