C++ Solution by Binary Search


  • 0
    M
    #include <cstdint>
    
    class Solution {
    public:
        int mySqrt(int x) {
            int64_t l = 0, r = x, m = 0;
            while (l < r) {
                m = (l + r + 1) / 2;
                if (m * m > x) r = m - 1; else l = m;
            }
            return l;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.