首页 > 其他 > 详细

Sqrt(x)

时间:2015-06-03 06:06:47      阅读:239      评论:0      收藏:0      [点我收藏+]

看cast type比较重要,

public class Solution {
    public int sqrt(int x) {
        // 巧妙使用二分法
        // ref http://www.cnblogs.com/springfor/p/3857772.html
        
        int low =0, high=x;
        while(low<=high){
            long mid = (long) low+(high-low)/2;
            if(mid*mid==x) return (int) mid;
            if(mid*mid<x){
                low = (int) mid+1;
            }else high = (int) mid-1;
        }
        return high;
    }
}

 

Sqrt(x)

原文:http://www.cnblogs.com/jiajiaxingxing/p/4548180.html

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