剑指offer面试题16. 数值的整数次方(二分法)

题目描述

实现函数double Power(double base, int exponent),求base的exponent次方。不得使用库函数,同时不需要考虑大数问题。
在这里插入图片描述

思路

详见链接

代码

class Solution:
	def myPow(self,x:float,n:int)->float:
		if x == 0:
			return 0
		res = 1
		if n < 0:
			x, n = 1/x, -n
		while n:
			x *= x
			n >>= 1  #(n除以2)
			if n & 1:
				res *= x
		return res
发布了227 篇原创文章 · 获赞 633 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_37763870/article/details/105548144
今日推荐