Implement int sqrt(int x)
.
Compute and return the square root of x.
1 class Solution { 2 public: 3 int mySqrt(int x) { 4 long r = x; 5 while (r*r > x) 6 r = (r + x/r) / 2; 7 return r; 8 } 9 };
时间: 2024-10-24 20:49:55
Implement int sqrt(int x)
.
Compute and return the square root of x.
1 class Solution { 2 public: 3 int mySqrt(int x) { 4 long r = x; 5 while (r*r > x) 6 r = (r + x/r) / 2; 7 return r; 8 } 9 };