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



版权声明:本文为weixin_41545780原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。