leetcode---sqrtx---分治

来源:互联网 发布:外汇交易软件下载 编辑:程序博客网 时间:2024/06/05 23:48

Implementint sqrt(int x).
Compute and return the square root of x.

class Solution {public:    int sqrt(int x)     {        unsigned long long low = 0;        unsigned long long high = x;        while(low <= high)        {            unsigned long long mid = (low + high) / 2;            unsigned long long int tmp = mid * mid;            if(tmp == x)                return mid;            else if(tmp < x)                low = mid + 1;            else                high = mid - 1;        }        return high;    }};