码迷,mamicode.com
首页 > 其他好文 > 详细

[Math]Sqrt(x)

时间:2015-12-14 01:19:32      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

Total Accepted: 75767 Total Submissions: 314003 Difficulty: Medium

 

Implement int sqrt(int x).

Compute and return the square root of x.

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

[Math]Sqrt(x)

标签:

原文地址:http://www.cnblogs.com/zengzy/p/5043931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!