File tree 3 files changed +37
-5
lines changed
3 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ package main
5
5
* @Date: 2019-09-22 15:05:54
6
6
* @LastEditors: wangtongli
7
7
8
- * @LastEditTime: 2019-09-22 16:17:14
8
+ * @LastEditTime: 2019-09-22 23:40:37
9
9
*/
10
10
11
11
import (
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
/*
4
- * @Description: "sqrt function"
4
+ * @Description: 69.Sqrt(x)
5
5
* @Date: 2019-09-22 16:08:09
6
6
* @LastEditors: wangtongli
7
7
8
- * @LastEditTime: 2019-09-22 16:21:02
8
+ * @LastEditTime: 2019-09-22 23:42:13
9
9
*/
10
-
11
10
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
13
21
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments