#力扣 LeetCode704. 二分查找 @FDDLC
程序员文章站
2022-03-09 15:21:13
...
题目描述:
https://leetcode-cn.com/problems/binary-search/
Java代码:
class Solution { //所有元素是不重复的
public int search(int[] nums, int target) {
int l=0,r=nums.length-1,m;
while(l<=r){
m=l+(r-l)/2;
if(nums[m]>target)r=m-1;
else if(nums[m]<target)l=m+1;
else return m;
}
return -1;
}
}