Leetcode Binary search二分法算法相关题的java实现和解析

Leetcode Binary search二分法算法相关题的java实现和解析(第一篇)

上一次做了二叉树的相关的题目的总结和java实现,然后这一次我们来处理二分法的相关的题目,严格来说二分法都是有通用的模板的,一般来说对于时间复杂度要求为 O ( n l o g n ) O(nlogn) O(nlogn) 的题目多半都可以用二分法来解决。

让我们看看题目列表吧:

  • Binary search(First position)
  • Binary search(Last postion)
  • Search and insert position
  • Find minimum in rotated sorted array
  • Find the first and last element
  • First bad version

Binary search(First position)

一个数组中可能有重复的数字,找到第一个符合我们要求的数字

class Solution{
	public int binarySearch(int[] nums, int target){
		if(nums == null || nums.length == 0)return 0;
		int start = 0, end = nums.length-1;
		while(start + 1 < end){
			int mid = (end-start)/2 + start;
			if(nums[mid] >= target) end = mid;
			else{
				start = mid;
			}
		}
		if(nums[start] == target) return start;
		if(nums[end] == target) return end;
		return -1;
	}
}

Binary search(Last postion)

class Solution{
	public int BinarySearch(int[] nums, int target){
		if(nums == null || nums.length == 0)return 0;
		int start = 0, end = nums.length-1;
		while(start+ 1 < end){
			int mid = (end-start)/2 +start;
			if(nums[mid] <= target) start = mid;
			else[
				end = mid;
			}
		}
		if(nums[end] == target) return end;
		if(nusm[start] == target) return start;
		return -1;
	}
}

Search insert position

class solution{
	public int searchInsert(int[] nums,int target){
		if(nums == null || nums.length == 0)return 0;
		int start = 0, end = nums.length-1;
		if(nums[start] > target) return start;
		if(nums[end] < target) return end+1;
		while(start +1 < end){
			int mid = (end-start)/2 + start;
			if(nums[mid] < target)start = mid;
			else{
				end = mid;
			}
		}
		if(nums[start] == target)return start;
		return end;
	}
}

Find minimum in rotated sorted array

这一题就是要找到一个旋转过的排序数组里面第一个小于当前数组里面最后一个元素的元素

class Solution{
	public int minRSA(int[] nums)[
		if(nums == null|| nums.length == 0)return 0;
		int start = 0, end = nums.length -1;
		int target = nums[end];
		while(start + 1 < end){
			int mid = (end-start)/2 + start;
			if(nums[mid] <= target)end = mid;
			else{
				start = mid;
			}
		}
		if(nums[start] <= target) return start;
		return end;
	}
}

Find the first and last element

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] res = new int[2];
        if(nums == null || nums.length == 0){
            res[0] = -1;
            res[1] = -1;
            return res;
        }
		int first = helper(nums,target,true);
		if(first == nums.length || nums[first] != target){
            res[0] = -1;
            res[1] = -1;
            return res;
        }
		res[0] = first;
		res[1] = helper(nums,target,false);
        return res;
    }
    protected int helper(int[] nums,int target,boolean first){
		int start = 0, end = nums.length-1;
		while(start + 1 < end){
			int mid = (end-start)/2+start;
			if(first){
				if(nums[mid] >= target)end = mid;
				else{start = mid;}
			}else{
				if(nums[mid] <= target) start = mid;
				else{end = mid;}
			}
		}
		if(first){
			if(nums[start] == target) return start;
			return end;
		}
		else{
			if(nums[end] == target) return end;
			return start;
		}
	}
}

First bad version

这一题其实是Search in Big sorted Array ,如果我们不知道当前的数组的大小,那我们该如何寻找我们希望找到的目标呢?

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int start = 0,end = n;
        
        while(start + 1 < end){
            int mid = (end-start)/2 + start;
            if(isBadVersion(mid) == false){
                start = mid;
            }else{
                end = mid;
            }
        }
        if(isBadVersion(end) == true){
            return end;
        }
        if(isBadVersion(start) == true){
            return start;
        }
        return end;
    }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页