We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9006a52 commit 1360e36Copy full SHA for 1360e36
solution/0069.Sqrt(x)/Solution2.go
@@ -0,0 +1,14 @@
1
+func mySqrt(x int) int {
2
+ precision := 0.9 // 计算精度, 但 LeetCode 中要求返回 int 所以可以偷懒将误差 < 0.9 即可(不使用 1.0 是因为浮点型的精度问题), 追求精度可使用 0.0001
3
+ guess, check := 1.0, 0.0
4
+calc:
5
+ guess = (float64(x)/guess + guess) / 2
6
+ check = guess*guess - float64(x)
7
+ if check < 0 {
8
+ check = 0 - check
9
+ }
10
+ if check > precision {
11
+ goto calc
12
13
+ return int(guess)
14
+}
0 commit comments