sqrtx

  • 2022-12-14
  • 浏览 (549)

sqrtx.cpp 源码

// x的平方根

class Solution {
public:
    int mySqrt(int x) {
        int low = 1, high = x;
        while (low <= high){
            int mid = low + (high - low) / 2;
            if (mid < x/mid) {
                low = mid + 1;
            } else if (mid > x/mid) {
                high = mid - 1;
            } else {
                return mid;
            }
        }
        return high;
    }
};

你可能感兴趣的文章

find_min_rotated_array

first_bad_version

first_last_in_array

0  赞