题解 | #在旋转过的有序数组中寻找目标值#
在旋转过的有序数组中寻找目标值
https://www.nowcoder.com/practice/87c0e7abcbda41e7963660fa7d020995
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ public int search (int[] nums, int target) { int length = nums.length; if (length == 0) { return -1; } int first = nums[0]; int last = nums[length - 1]; if (last < target && target < first) { return -1; } if (target >= first) { for (int i = 0; i < length; i++) { if (nums[i] == target) { return i; } if (nums[i] < first) { break; } } } if (target <= last) { for (int i = length - 1; i >= 0; i--) { if (nums[i] == target) { return i; } if (nums[i] > last) { break; } } } return -1; } }#找工作##刷题#