Merge pull request #191 from qq502361472/master

更新0704.二分查找,java版本
This commit is contained in:
Carl Sun 2021-05-19 23:33:27 +08:00 committed by GitHub
commit c98c8fb385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 0 deletions

View File

@ -153,6 +153,10 @@ Java
```java
class Solution {
public int search(int[] nums, int target) {
// 避免当 target 小于nums[0] nums[nums.length - 1]时多次循环运算
if (target < nums[0] || target > nums[nums.length - 1]) {
return -1;
}
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);