Sword Finger Offer Interview Question 16. Integer Power of Numbers (Dichotomy)

Title description

Implement the function double Power (double base, int exponent), find the exponent power of base. Don't use library functions, and don't need to consider large numbers.
Insert picture description here

Ideas

See link for details

Code

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
Published 227 original articles · praised 633 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_37763870/article/details/105548144