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

LeetCode 69: Sqrt(x)

时间:2017-08-31 16:19:00      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:tar   while   log   star   etc   public   int   logs   end   

class Solution {
    public int mySqrt(int x) {
        if (x < 0) {
            return -1;
        }
        
        if (x < 2) {
            return x;
        }
        int start = 1;
        int end = x;
        while (end - start > 1) {
            int mid = start + (end - start)/2;
            if (mid == x / mid) {
                return mid;
            } else if (mid > x / mid) {
                end = mid;
            } else {
                start = mid;
            }
        }
        return start;
    }
}

 

LeetCode 69: Sqrt(x)

标签:tar   while   log   star   etc   public   int   logs   end   

原文地址:http://www.cnblogs.com/shuashuashua/p/7458297.html

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