Skip to content

69. x 的平方根  #36

Open
Open
@zpc7

Description

@zpc7

69. x 的平方根

/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function (x) {
    // 注意题目只需要返回整数部分
    // 找到中间节点
    let left = 0;
    let right = Math.trunc(x / 2);

    while (left <= right) {
        const rightValue = right * right;
        if (rightValue === x) return right;
        if (rightValue > x) {
            right = Math.trunc(right / 2);
        } else {
            left = right;
            right = right + 1

            if (left * left < x && right * right > x) return left;
        }
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions