We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Difficulty: 简单
Related Topics: 数学, 二分查找
给你一个非负整数 x ,计算并返回 x 的 算术平方根 。
x
由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。
**注意:**不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。
pow(x, 0.5)
x ** 0.5
示例 1:
输入:x = 4 输出:2
示例 2:
输入:x = 8 输出:2 解释:8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
提示:
Language: JavaScript
/** * @param {number} x * @return {number} */ // 「x的平方根」 二分 var mySqrt = function(x) { let left = 0 let right = x while (left <= right) { let mid = left + ((right - left) >>> 1) if (mid * mid < x) { left = mid + 1 } else if (mid * mid > x) { right = mid - 1 } else { return mid } } return right };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
69. x 的平方根
Description
Difficulty: 简单
Related Topics: 数学, 二分查找
给你一个非负整数
x
,计算并返回x
的 算术平方根 。由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。
**注意:**不允许使用任何内置指数函数和算符,例如
pow(x, 0.5)
或者x ** 0.5
。示例 1:
示例 2:
提示:
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: