Skip to content

Commit 0955e80

Browse files
committed
69
1 parent a55b4ed commit 0955e80

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package main
55
* @Date: 2019-09-22 15:05:54
66
* @LastEditors: wangtongli
77
8-
* @LastEditTime: 2019-09-22 16:17:14
8+
* @LastEditTime: 2019-09-22 23:40:37
99
*/
1010

1111
import (

mysqrt.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package main
22

33
/*
4-
* @Description: "sqrt function"
4+
* @Description: 69.Sqrt(x)
55
* @Date: 2019-09-22 16:08:09
66
* @LastEditors: wangtongli
77
8-
* @LastEditTime: 2019-09-22 16:21:02
8+
* @LastEditTime: 2019-09-22 23:42:13
99
*/
10-
1110
func mySqrt(x int) int {
12-
return 0
11+
var i, value int
12+
//初始化,执行一次;条件,必须;其他
13+
for value < x {
14+
i++
15+
value = i * i
16+
}
17+
if value > x {
18+
return i - 1
19+
}
20+
return i
1321
}

mysqrt1.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
/*
4+
* @Description: 69.Sqrt(x)
5+
* @Date: 2019-09-22 23:38:59
6+
* @LastEditors: wangtongli
7+
8+
* @LastEditTime: 2019-09-22 23:41:55
9+
*/
10+
func mySqrt1(x int) int {
11+
max, min := x, 0
12+
if x == 1 {
13+
return 1
14+
}
15+
for max-min > 1 {
16+
mid := (min + max) / 2
17+
if mid*mid > x {
18+
max = mid
19+
} else {
20+
min = mid
21+
}
22+
}
23+
return min
24+
}

0 commit comments

Comments
 (0)