Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

public class Solution {
    public int sqrt(int x) {
        int start = 0;
        int end = x;
        
        if(x == 0 ||  x == 1) return x;
        
        while(start <= end){
            int m = (start+end)/2;
            if(m == x/m){
                return m;
            }else if(m > x/m){
                end = m -1;
            }else{
                start = m+1;
            }
            
        }
        
        return (start+ end)/2;
    }
}
原文地址:https://www.cnblogs.com/RazerLu/p/3535775.html