Leetcode刷题记录——69. x 的平方根

在这里插入图片描述

二分查找

class Solution:
    def mySqrt(self, x: int) -> int:
        if x == 0:
            return 0
        elif x == 1:
            return 1
        end = x // 2 + 1
        print(end)
        start = 0
        while True:
            temp = (end + start) // 2
            print(temp)
            if temp*temp == x or (temp*temp < x and (temp+1)*(temp+1) >x):
                return temp
            elif temp*temp < x :
                start = temp + 1
            elif temp*temp > x:
                end = temp - 1


发布了59 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41545780/article/details/105480240