题解 | #求平方根#

求平方根

http://www.nowcoder.com/practice/09fbfb16140b40499951f55113f2166c

import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return int整型
     */
    public int sqrt (int x) {
        int l = 0, r = x;
        while(l <= r){
            int mid = (l+r) >> 1;
            long tem = (long)mid * mid;
            if(tem > x){
                r = mid - 1;
            }else if(tem < x){
                l = mid + 1;
            }else{
                return mid;
            }
        }
        return r;
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务