Find the last position of a target number in a sorted array. Return -1 if target does not exist.
Example Given [1, 2, 2, 4, 5, 5].
For target = 2, return 2.
For target = 5, return 5.
For target = 6, return -1.
思路
二分查找。
注意在nums[mid] == target的时候,说明找到了一个。我们是要找最后一个target,所以要从这个找到的地方开始往后看,看还有没有。所以start = mid。
Code
public class Solution { /** * @param nums: An integer array sorted in ascending order * @param target: An integer * @return an integer */ public int lastPosition(int[] nums, int target) { // Write your code here if (nums == null || nums.length == 0) { return -1; } int start = 0; int end = nums.length - 1; while (start + 1 < end) { int mid = start + (end - start) / 2; if (nums[mid] > target) { end = mid; } else if (nums[mid] < target) { start = mid; } else { start = mid; } } if (nums[end] == target) { return end; } if (nums[start] == target) { return start; } return -1; } }