69. Sqrt(x)

来源:互联网 发布:微商裂变软件 编辑:程序博客网 时间:2024/05/23 12:30

Implement int sqrt(int x).

Compute and return the square root of x.

public class Solution {    public int mySqrt(int x) {        if (x == 0) return 0;        int start = 1, end = x;        while (start <= end) {            int mid = start + (end - start) / 2;            if (mid <= x / mid && (mid + 1) > x / (mid + 1))                 return mid;             else if (mid > x / mid)                end = mid - 1;            else    start = mid + 1;    }    return start;    }}