leetcode 1033. 搜索旋转排序数组

    科技2022-07-11  86

    class Solution { public int search(int[] nums, int target) { int n = nums.length; int l = 0, r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (nums[mid] == target) return mid; if(nums[0]==target) return 0; if(nums[n-1]==target) return n-1; if(nums[l]==target) return l; if(nums[r]==target) return r; if (nums[0] < nums[mid]) { if (nums[0] < target && target < nums[mid]) r = mid - 1; else l = mid + 1; } else if (nums[0] >= nums[mid]) { if (nums[0] > target && target > nums[mid]) l = mid + 1; else r = mid - 1; } } return -1; } }

     

    Processed: 0.011, SQL: 8