计算x的平方根

public class Solution {
    public int mySqrt(int a) {
        if(a < 2) return a;
        int start = 2;
        int end = a/2;
        int mid = 0;
        while(start <= end) {
            mid = start + (end - start)/2;
            long num = (long)mid*mid;
            if(num > a) end = mid-1;
            else if(num < a) start = mid+1;
            else return mid;    
        }        
        return end;
    }
}

  https://blog.csdn.net/weixin_43306331/article/details/103796284

posted @ 2023-10-29 20:06  樱圃  阅读(3)  评论(0编辑  收藏  举报