1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @Title: x 的平方根 (Sqrt(x))
// @Author: 15816537946@163.com
// @Date: 2020-06-25 07:15:36
// @Runtime: 0 ms
// @Memory: 2.2 MB
func mySqrt(x int) int {
	if x <= 1 {
		return x
	}
	lo, hi :=1, x
	for lo <= hi {
		mid := lo+(hi-lo)/2
		sqrt := x/mid
		if sqrt == mid {
			return mid
		} else if sqrt > mid {
			lo = mid+1
		} else {
			hi = mid-1
		}
	}
	return hi
    
}