算法:Find First and Last Position of Element in Sorted Array(在有序数组中搜索第一个和最后一个找到的数字)

说明

算法: Find First and Last Position of Element in Sorted Array
地址:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm’s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

解法一

创建两个独立的方法:找到第一个,和找到最后一个。

public int[] searchRangeWithEasyTwoMethod(int[] nums, int target) {
    int[] result = new int[]{-1, -1};
    // edge check
    if (nums == null || nums.length == 0) {
      return result;
    }
    result[0] = findFisrt(nums, target);
    result[1] = findLast(nums, target);

    return result;
  }

  private int findFisrt(int[] nums, int target) {
    int result = -1;
    int left = 0;
    int right = nums.length - 1;
    while (left <= right) {
      int mid = left + ((right - left) >> 1);
      if (nums[mid] >= target) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
      if (nums[mid] == target) {
        result = mid;
      }
    }

    return result;
  }

  private int findLast(int[] nums, int target) {
    int result = -1;
    int left = 0;
    int right = nums.length - 1;
    while (left <= right) {
      int mid = left + ((right - left) >> 1);
      if (nums[mid] <= target) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
      if (nums[mid] == target) {
        result = mid;
      }
    }


    return result;
  }

解法二

  1. 找到第一个大于等于target的位置,也就是左边的起始值。
    陷阱1:low <= target < high. 如果target比所有值都大,则返回值会是array.length。
    陷阱2:如果没有这个值,那么返回来的low,nums[low] != target
  2. 找到第一个大于等于target+1的位置,也就是右边的起始值,记住要结果-1.
public int[] searchRangeWithFirstGreatEqual(int[] nums, int target) {
    int[] result = new int[]{-1, -1};
    // edge check
    if (nums == null || nums.length == 0) {
      return result;
    }
    int find = findFirstGreatEqual(nums, target);
    if (find >= nums.length || nums[find] != target) {
      return result;
    }
    result[0] = find;
    result[1] = findFirstGreatEqual(nums, target + 1) - 1;

    return result;
  }

  //find the first number that is greater than or equal to target.
  //could return A.length if target is greater than A[A.length-1].
  private int findFirstGreatEqual(int[] nums, int target) {
    int low = 0;
    int high = nums.length;
    while (low < high) {
      int mid = low + ((high - low) >> 1);
      //low <= mid < high
      if (nums[mid] < target) {
        low = mid + 1;
      } else {
        //should not be mid-1 when A[mid]==target.
        //could be mid even if A[mid]>target because mid
        high = mid;
      }
    }

    return low;
  }

代码下载

https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/binarysearch/FindFirstAndLastPositionOfElementInSortedArray.java

你可能感兴趣的:(算法)