Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 371 Bytes

69.sqrtx.md

File metadata and controls

22 lines (18 loc) · 371 Bytes

解题思路

两种二分模板的选择

[l, mid-1], [mid, r] 和 [l, mid], [mid+1, r]

代码

class Solution {
public:
    int mySqrt(int x) {
        long long l = 0, r = x;
        while (l < r){
            long long mid = l + r + 1 >> 1;
            if (mid * mid <= x) l = mid;
            else r = mid - 1;
        }
        return l;
    }
};