Skip to content

x 的平方根  #22

Description

@YalongYan

题目地址:69. x 的平方根

思路:

二分法

代码:

var mySqrt = function(x) {
    var lo=0, hi=x, mid;
    while(lo <= hi){
        mid = Math.floor((lo+hi)/2)
        if(mid * mid === x) return mid;
        if(mid*mid<x){
            lo=mid+1
        }else{
            hi=mid-1;
        }
    }
    //  return Math.floor(hi);
     return hi;
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    算法New feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions