1-数组-14-x 的平方根- LeetCode69
LeetCode:题目序号69
更多内容欢迎关注我(持续更新中,欢迎Star✨)
Github:CodeZeng1998/Java-Developer-Work-Note
技术公众号:CodeZeng1998(纯纯技术文)
生活公众号:好锅(Life is more than code)
掘金: CodeZeng1998
其他平台:CodeZeng1998、好锅
给你一个非负整数 x ,计算并返回 x 的 算术平方根 。
由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。
注意: 不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。
示例 1:
输入:x = 4
输出:2
示例 2:
Input: x = 8 Output: 2 Explanation: The arithmetic square root of 8 is 2.82842..., since the return type is an integer, the decimal part will be rounded off.
提示:
0 <= x <= 231 - 1
/**
* x 的平方根(左闭右闭)[left, right]
*
* @param x 待获取的值
* @return 返回符合条件的值
* @Author: CodeZeng1998
*/
public int sqrt(int x) {
// 一个整数的平方根肯定不会超过它自己的一半,但是 0 和 1 除外,需特殊处理
if (x == 0 || x == 1) {
return x;
}
int left = 1;
int right = x >> 1;
while (left <= right) {
int middle = left + ((right - left) >> 1);
// long 强制转化,防止 int 溢出
if ((long) middle * middle > x) {
right = middle - 1;
} else if ((long) middle * middle < x) {
left = middle + 1;
} else {
return middle;
}
}
return right;
}
ChatGPT:
这段代码是一个用二分查找法求一个整数的平方根的函数。以下是对代码的详细解释:
sqrt(int x)方法接收一个整数x作为输入,返回x的平方根。- 首先,程序对特殊情况进行了处理:当
x为 0 或 1 时,它们的平方根分别也是 0 和 1,因此直接返回x。 - 然后,程序初始化了两个变量
left和right。left初始化为 1,因为任何正整数的平方根都不会小于 1。right初始化为x的一半,因为一个整数的平方根不会超过它自己的一半,但要注意 0 和 1 的情况已经在上一步处理了。 - 接下来是二分查找的主循环。在每次迭代中,程序计算
middle,它是left和right之间的中间值。通过(right - left) >> 1计算中间值可以防止在计算middle时发生整数溢出。 - 在每次迭代中,程序通过将
middle的平方与x进行比较,来确定二分查找的下一步方向。如果middle * middle大于x,则将right更新为middle - 1,因为平方根一定在left和middle - 1之间。如果middle * middle小于x,则将left更新为middle + 1,因为平方根一定在middle + 1和right之间。如果middle * middle等于x,则找到了平方根,直接返回middle。 - 循环继续直到
left > right,此时返回right,因为在这种情况下,right的值即为x的平方根的整数部分。
这段代码利用了二分查找的思想,在时间复杂度上达到了 O(logn),相比于简单的遍历,效率更高。
Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
You must not use any built-in exponent function or operator.
- For example, do not use
pow(x, 0.5)in c++ orx ** 0.5in python.
Example 1:
Input: x = 4
Output: 2
Explanation: The square root of 4 is 2, so we return 2.
Example 2:
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
Constraints:
0 <= x <= 231 - 1
更多内容欢迎关注我(持续更新中,欢迎Star✨)
Github:CodeZeng1998/Java-Developer-Work-Note
技术公众号:CodeZeng1998(纯纯技术文)
生活公众号:好锅(Life is more than code)
掘金: CodeZeng1998
其他平台:CodeZeng1998、好锅