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件 訪問者20,000人以上

おすすめ

転載: blog.csdn.net/weixin_41545780/article/details/105480240