[leetcode]Sqrt(x)

class Solution {
public:
    int sqrt(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x < 1) return 0;
        if(x == 1) return 1;
        
        int start = 1;
        int end = x;
        
        int mid;
        while(start <= end){
            mid = (start + end)/2;
            
            if(x / mid == mid) return mid;
            if(mid < x/mid) start = mid +1;
            else end = mid -1;
            
        }
        
        return (start + end)/2;
    }
};


原文地址:https://www.cnblogs.com/aukle/p/3215118.html