文章目录
- 正常实现
- [69 x 的平方根](https://leetcode-cn.com/problems/sqrtx/submissions/)
- [744 寻找比目标字母大的最小字母](https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target/submissions/)
- [540 有序数组中的单一元素](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/submissions/)
- [278 第一个错误的版本](https://leetcode-cn.com/problems/first-bad-version/submissions/)
- [153 寻找旋转排序数组中的最小值](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/submissions/)
- [154. 寻找旋转排序数组中的最小值 II](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/submissions/)
- [34 在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/)
正常实现
public int binarySearch(int[] nums, int key) {
int l=0,h=nums.length-1;
while(l<=h){
int m=l+(h-l)/2;
if(nums[m]==key){
return m;
}else if(nums[m]<key){
l=m+1;
}else{
h=m-1;
}
}
return -1;
}
69 x 的平方根
public int mySqrt(int x) {
if(x<=1)
return x;
int l=1,h=x;
while(l<=h){
int m=l+(h-l)/2;
int sqrt=x/m;
if(m==sqrt){
return m;
}else if(m<sqrt){
l=m+1;
}else{
h=m-1;
}
}
return h;
}
牛顿法,令 f ( x ) = x 2 − t f(x)=x^2-t f(x)=x2−t
public int mySqrt(int x) {
if(x<=1)
return x;
long r=x;
while(r>x/r){
r=(r+x/r)/2;
}
return (int)r;
}
744 寻找比目标字母大的最小字母
public char nextGreatestLetter(char[] letters, char target) {
int l=0,h=letters.length-1;
if(letters[h]<=target)
return letters[0];
while(l<=h){
int m=l+(h-l)/2;
if(letters[m]<=target){
l=m+1;
}else{
h=m-1;
}
}
return letters[l];
}
540 有序数组中的单一元素
public int singleNonDuplicate(int[] nums) {
if(nums==null || nums.length==0)
return -1;
int l=0,h=nums.length-1;
while(l<h){
int m=l+(h-l)/2;
if(m%2==1){
m--;// 保证 l/h/m 都在偶数位,使得查找区间大小一直都是奇数
}
if(nums[m]==nums[m+1]){
l=m+2;
}else if(m==0 || nums[m]!=nums[m-1]){
return nums[m];
}else{
h=m-2;
}
}
return nums[l];
}
278 第一个错误的版本
即等同于找到第一个等于某个值,注意与查找第一个大于某个数字的区别。
- nums[m] > nums[h] l=m+1
- nums[m] <= nums[h] h=m-1 //终止条件为l <= h
public int firstBadVersion(int n) {
if(n<=0)
return -1;
int l=1,h=n;
while(l<=h){
int m=l+(h-l)/2;
if(isBadVersion(m)){
h=m-1;
}else{
l=m+1;
}
}
return l;
}
153 寻找旋转排序数组中的最小值
- nums[m] > nums[h] l=m+1
- nums[m] <= nums[h] h=m //终止条件为l < h
public int findMin(int[] nums) {
if(nums==null || nums.length==0)
return -1;
int l=0,h=nums.length-1;
while(l<h){
int m=l+(h-l)/2;
if(nums[m]>nums[h]){
l=m+1;
}else{
h=m;//此时nums[m]可能为最小的时
}
}
return nums[l];
}
154. 寻找旋转排序数组中的最小值 II
主要是解决碰到[2,2,2,0,2]和[2,0,2,2,2]这种情况时该怎么移动指针的问题。
还是和最右边的元素比较:
- nums[m] > nums[h] l=m+1
- nums[m] < nums[h] h=m //终止条件为l < h
- 其他 h=h-1
public int findMin(int[] nums) {
int l=0, h= nums.length-1;
while(l < h){
int m = (l + h)/2;
if(nums[m] > nums[h]){
l = m +1;
}else if(nums[m] < nums[h]){
h = m;
}else{
h = h - 1;
}
}
return nums[l];
}
34 在排序数组中查找元素的第一个和最后一个位置
可以看作是求被查找元素的第一个位置和被查找元素+1的第一个位置
public int[] searchRange(int[] nums, int target) {
int first=binarySearch(nums,target);
int last=binarySearch(nums,target+1)-1;
if(first==nums.length||nums[first]!=target){
return new int[]{-1,-1};
}else{
return new int[]{first,last};
}
}
//查找第一个等于target的位置
public int binarySearch(int []nums,int target){
int l = 0, h = nums.length - 1;
while (l <= h) {
int m = l + (h - l) / 2;
if (nums[m] >=target) {
h = m-1;
} else if(nums[m]<target) {
l = m + 1;
}
}
return l;//若不存在,则可能返回0(nums={1,2},target=0)或nums.length(nums={1,2},target=3}或第一个大于target的值
}