难度:中等 题目来源:力扣(LeetCode) https://leetcode-cn.com/problems/powx-n/
说明:
实现 pow(x, n) ,即计算 x 的 n 次幂函数。
示例:
示例 1:
输入:2.00000, 10 输出:1024.00000
示例 2:
输入:2.10000, 3 输出:9.26100
示例 3:
输入:2.00000, -2 输出:0.25000
解释:2 = 1/2 = 1/4 = 0.25
解法:
func myPow(x float64, n int) float64 {if x == 0 {return 0}if n == 0 || x == 1 {return 1}if n < 0 {x = 1 / xn = -n}helf := myPow(x, n/2)if n%2 == 1 {return helf * helf * x}return helf * helf}
