Skip to content

Commit 853e4e9

Browse files
add Solution 0069 [golang]
1 parent bbace09 commit 853e4e9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

solution/0069.Sqrt(x)/Solution.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @lc app=leetcode.cn id=69 lang=golang
3+
* Accepted
4+
* 1017/1017 cases passed (0 ms)
5+
* Your runtime beats 100 % of golang submissions
6+
* Your memory usage beats 25.49 % of golang submissions (2.2 MB)
7+
*/
8+
9+
func mySqrt(x int) int {
10+
if x == 0 || x == 1 {
11+
return x
12+
}
13+
l, r, t := 1, x, 0
14+
m := (l + r) / 2
15+
for l != m {
16+
t = m * m
17+
if t == x {
18+
return m
19+
}
20+
if t < x {
21+
l = m
22+
} else {
23+
r = m
24+
}
25+
m = (l + r) / 2
26+
}
27+
return m
28+
}

0 commit comments

Comments
 (0)