数值的整数次方 递归

 

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)
	}
}

  

 

posted @ 2022-04-29 23:50  papering  阅读(27)  评论(0)    收藏  举报