Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b6e7f9

Browse files
committed
Merge branch 'master' of https://github.com/doocs/leetcode
2 parents 40a2e51 + 47a5d81 commit 8b6e7f9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

‎solution/0069.Sqrt(x)/Solution2.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /