69. Sqrt(x)

Implementint 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 begin = 1;
        int end = x;
        int mid = 0;
        while(begin<=end)
        {
            mid = (begin + end)>>1;
            if(mid==x/mid)
              return mid;
            else if(midx/mid)
              end = mid -1;
        }
        return end;    //为什么返回的是end???
    }
}

你可能感兴趣的:(69. Sqrt(x))