题目
法1:二分查找
根据mid来分段,此思路需要牢记!!!
class Solution {public int search(int[] nums, int target) {if (nums.length == 0) {return -1;}int left = 0, right = nums.length - 1, mid = 0;while (left <= right) {mid = left + (right - left) / 2;if (nums[mid] == target) {return mid;}if (nums[mid] >= nums[left]) { // mid在左段if (target >= nums[left] && target < nums[mid]) {right = mid - 1;} else {left = mid + 1;}} else { // mid在右段if (target <= nums[right] && target > nums[mid]) {left = mid + 1;} else {right = mid - 1;}}}return -1;}
}