mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-04 23:02:31 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -140,7 +140,6 @@ func binarySearch(nums []int, target, left, right int) int {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
二分查找的思路及代码已经理解了,那么我们来看一下实现时容易出错的地方
|
||||
|
||||
1.计算 mid 时 ,不能使用 (left + right )/ 2,否则有可能会导致溢出
|
||||
@@ -157,7 +156,7 @@ Java Code:
|
||||
|
||||
```java
|
||||
public static int binarySearch(int[] nums,int target,int left, int right) {
|
||||
|
||||
|
||||
if (left <= right) {
|
||||
int mid = left + ((right - left) >> 1);
|
||||
if (nums[mid] == target) {
|
||||
|
Reference in New Issue
Block a user