-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSqrtx.java
More file actions
63 lines (59 loc) · 1.91 KB
/
Copy pathSqrtx.java
File metadata and controls
63 lines (59 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* <p>给你一个非负整数 <code>x</code> ,计算并返回 <code>x</code> 的 <strong>算术平方根</strong> 。</p>
*
* <p>由于返回类型是整数,结果只保留 <strong>整数部分 </strong>,小数部分将被 <strong>舍去 。</strong></p>
*
* <p><strong>注意:</strong>不允许使用任何内置指数函数和算符,例如 <code>pow(x, 0.5)</code> 或者 <code>x ** 0.5</code> 。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>x = 4
* <strong>输出:</strong>2
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>x = 8
* <strong>输出:</strong>2
* <strong>解释:</strong>8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>0 <= x <= 2<sup>31</sup> - 1</code></li>
* </ul>
* <div><div>Related Topics</div><div><li>数学</li><li>二分查找</li></div></div><br><div><li>👍 842</li><li>👎 0</li></div>
*/
package leetcode4;
public class Sqrtx {
public static void main(String[] args) {
Solution solution = new Sqrtx().new Solution();
}
/**
* mid取偏右的值,当大于x时,right直接限制到mid的左值。
* 计算mid时始终覆盖x,且最终mid始终是left值。
* <p>
* 注:注意溢出场景的处理,使用long类型:right - left + 1,当left为0时,就是right+1了
*/
class Solution {
public int mySqrt(int x) {
long left = 0, right = x;
while (left < right) {
long mid = (right - left + 1) / 2 + left;
if (mid * mid > x) {
right = mid - 1;
} else {
left = mid;
}
}
return (int) left;
}
}
}