Update solution 0069

This commit is contained in:
YDZ
2021-06-05 23:43:42 +08:00
parent d0c6f905f6
commit 640dc700a6
2 changed files with 8 additions and 15 deletions

View File

@ -11,7 +11,6 @@ func mySqrt(x int) int {
l = mid
}
}
return l
}

View File

@ -42,24 +42,18 @@ Since the return type is an integer, the decimal digits are truncated and only
package leetcode
// 解法一 二分
// 解法一 二分, 找到最后一个满足 n^2 <= x 的整数n
func mySqrt(x int) int {
if x == 0 {
return 0
}
left, right, res := 1, x, 0
for left <= right {
mid := left + ((right - left) >> 1)
if mid < x/mid {
left = mid + 1
res = mid
} else if mid == x/mid {
return mid
l, r := 0, x
for l < r {
mid := (l + r + 1) / 2
if mid*mid > x {
r = mid - 1
} else {
right = mid - 1
l = mid
}
}
return res
return l
}
// 解法二 牛顿迭代法 https://en.wikipedia.org/wiki/Integer_square_root