Leetcode brush test record-square root of 69. x

Insert picture description here

Binary search

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


Published 59 original articles · Liked 14 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/weixin_41545780/article/details/105480240