GoodTecher LeetCode Tutorial 69. Sqrt(x) (Java)
LeetCode Tutorial by GoodTecher. The tutorial provides a step-by-step demonstration on how to solve coding problems.
Complete Playlist for the GoodTecher LeetCode Tutorial:
GoodTecher Website:
GoodTecher Channel on YouTube:
GoodTecher Github:
Nguồn: https://chaoticpharmacology.com/
Xem thêm bài viết khác: https://chaoticpharmacology.com/cong-nghe/
bad solution, do not use this. instead check this solution:
class Solution {
public int mySqrt(int x) {
if (x < 2) return x;
long num;
int pivot, left = 2, right = x / 2;
while (left <= right) {
pivot = left + (right – left) / 2;
num = (long)pivot * pivot;
if (num > x) right = pivot – 1;
else if (num < x) left = pivot + 1;
else return pivot;
}
return right;
}
}
This code actually doesn’t work for the test case: X=8. It goes into an infinite loop. This is garbage code and I don’t think you even understand the solution.
For the case when x = 8, above code, goes into an infinite loop causing time limit to exceed.
why did you use lo+1 ?
看了很多不同的视频 你的概念思路是讲的最好的 非常清楚 感谢 请继续 并且可以把70题加上吗?