首页 > 其他 > 详细

x的平方根

时间:2015-09-05 11:10:12      阅读:232      评论:0      收藏:0      [点我收藏+]
class Solution {
public:
    /**
     * @param x: An integer
     * @return: The sqrt of x
     */
     int getResult(long start, long end, long target){
        long temp;
        while(start <= end){
            temp = (start + end) / 2;
            if(start * start > target)
                return start -1;
            if(end * end < target)
                return end;
            if(temp * temp == target)
                return temp;
            else if(temp * temp > target)
                end = temp -1;
            else if(temp * temp < target)
                start = temp + 1;
        }
    }
    int sqrt(int x) {
        // write your code here
        if(x == 0)
            return 0;
        else if(x <= 3)
            return 1;
        else if(x > 3)
            return getResult(3, x/2+1 ,x);
    }
};

ps:注意int越界

x的平方根

原文:http://www.cnblogs.com/smallby/p/4782838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!