其他分享
首页 > 其他分享> > 数值的整数次方 递归

数值的整数次方 递归

作者:互联网

 

https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/

func myPow(x float64, n int) float64 {
	var R func(x float64, m int) float64
	R = func(x float64, m int) float64 {
		if m == 0 {
			return 1
		}
		n := m % 2
		h := R(x, m/2)
		if n == 0 {
			return h * h
		} else {
			return h * h * x
		}
	}
	if n >= 0 {
		return R(x, n)
	} else {
		return 1 / R(x, -n)
	}
}

  

 

标签:return,shu,递归,float64,int,整数,else,func,次方
来源: https://www.cnblogs.com/rsapaper/p/16209162.html