Pow(x, n) Solutions in GoNumber 50Difficulty MediumAcceptance 30.4%Link LeetCodeOther languages C++, JavaSolutionsGo solution by halfrost/LeetCode-Gopackage 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}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 }