更新0704.二分查找,java版本
This commit is contained in:
parent
cbdfb41dd8
commit
22bd2751f6
|
|
@ -153,6 +153,10 @@ Java:
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int search(int[] nums, int target) {
|
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;
|
int left = 0, right = nums.length - 1;
|
||||||
while (left <= right) {
|
while (left <= right) {
|
||||||
int mid = left + ((right - left) >> 1);
|
int mid = left + ((right - left) >> 1);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue