-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q4_对x开根.java
50 lines (44 loc) · 990 Bytes
/
Q4_对x开根.java
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
package com.algorithm.demo.math;
/**
* 141 · 对x开根
* 题目题解笔记讨论排名
* 描述
* 实现 int sqrt(int x) 函数,计算并返回 x 的平方根。
* 样例
* 样例 1:
* 输入: 0
* 输出: 0
* 样例 2:
* 输入: 3
* 输出: 1
* 样例解释:
* 返回对x开根号后向下取整的结果。
* 样例 3:
* 输入: 4
* 输出: 2
*/
public class Q4_对x开根 {
public static void main(String[] args) {
}
/**
* @param x: An integer
* @return: The sqrt of x
*/
public int sqrt(int x) {
// write your code here
long X = (long) x;
long l = 1, r = X;
while (l + 1 < r) {
long mid = l + (r - l) / 2;
if (mid * mid == X) {
return (int) mid;
} else if (mid * mid < X) {
l = mid;
} else {
r = mid;
}
}
if (r * r == X) return (int) r;
return (int) l;
}
}