首页 > 其他 > 详细

[Leetcode]-- Sqrt(x)

时间:2014-01-28 22:26:34      阅读:743      评论:0      收藏:0      [点我收藏+]

Implement int sqrt(int x).

Compute and return the square root of x.

 

bubuko.com,布布扣
public class Solution {
    public int sqrt(int x) {
        int start = 0;
        int end = x;
        
        if(x == 0 ||  x == 1) return x;
        
        while(start <= end){
            int m = (start+end)/2;
            if(m == x/m){
                return m;
            }else if(m > x/m){
                end = m -1;
            }else{
                start = m+1;
            }
            
        }
        
        return (start+ end)/2;
    }
}
bubuko.com,布布扣

[Leetcode]-- Sqrt(x)

原文:http://www.cnblogs.com/RazerLu/p/3535775.html

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