Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

69. x 的平方根 #36

Open
zpc7 opened this issue Aug 20, 2022 · 0 comments
Open

69. x 的平方根 #36

zpc7 opened this issue Aug 20, 2022 · 0 comments
Labels
二分 简单 LeetCode 难度定级

Comments

@zpc7
Copy link
Owner

zpc7 commented Aug 20, 2022

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;
        }
    }
};
@zpc7 zpc7 added 简单 LeetCode 难度定级 二分 labels Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
二分 简单 LeetCode 难度定级
Projects
None yet
Development

No branches or pull requests

1 participant