forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySqrt.java
More file actions
38 lines (36 loc) · 803 Bytes
/
MySqrt.java
File metadata and controls
38 lines (36 loc) · 803 Bytes
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
package normal;
/**
* @program JavaBooks
* @description: 69. x 的平方根
* @author: mf
* @create: 2019/11/09 09:38
*/
/*
题目:https://leetcode-cn.com/problems/sqrtx/
难度:easy
*/
/*
输入: 4
输出: 2
输入: 8
输出: 2
说明: 8 的平方根是 2.82842...,
由于返回类型是整数,小数部分将被舍去。
*/
public class MySqrt {
public static void main(String[] args) {
System.out.println(mySqrt(8));
}
public static int mySqrt(int x) {
if (x <= 1) return x;
int l = 1, h = x;
while (l <= h) {
int mid = l + (h - l) / 2;
int sqrt = x / mid;
if (sqrt == mid ) return sqrt;
else if (mid > sqrt) h = mid - 1;
else l = mid + 1;
}
return h;
}
}