public class Solution {
public int MySqrt(int x) {
long r = x;
while (r * r > x)
r = (r + x / r) / ;
return (int)r;
}
}
https://leetcode.com/problems/sqrtx/#/description
补充一个python的实现:
class Solution:
def mySqrt(self, x: int) -> int:
r = x
while r * r > x:
r = (r + x // r) //
return int(r)
使用内置函数:
class Solution:
def mySqrt(self, x: int) -> int:
return int(x ** 0.5)