69. x的平方根
题目:

题解:
java
class Solution {
public int mySqrt(int x) {
int t = 0;
while((long)t*t <= x) {
t++;
}
return t-1;
}
}
后面我看了题解,发现大家都用了二分法,于是我也用了二分试试了,结果过了
但是我写二分还是不熟练,我只记得板子,条件总是不确定 总是需要试错 二分真的有亿点难
java
class Solution {
public int mySqrt(int x) {
if(x == 1){
return 1;
}
int l=0,r=x/2;
while(l<r) {
int mid=(l+r)/2+1;
if((long)mid*mid>x) {
r=mid-1;
}
else {
l=mid;
}
}
return l;
}
}