首页 > 其他分享 >33. 搜索旋转排序数组

33. 搜索旋转排序数组

时间:2022-11-07 00:00:44浏览次数:65  
标签:search return target nums 33 int l2 数组 排序

33. 搜索旋转排序数组

class Solution {
   public int search(int[] nums, int target) {
        int n = nums.length;
        if (n == 1) {
            return target == nums[0] ? 0 : -1;
        }
        int l1 = 0;
        int l2 = 0;
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                l2 = i;
                break;
            }
        }
        int index1 = search(nums, l1, l2, target);
        int index2 = search(nums, l2 + 1, n - 1, target);
        if (index1 != -1) {
            return index1;
        }
        if (index2 != -1) {
            return index2;
        }
        return -1;
    }
}

标签:search,return,target,nums,33,int,l2,数组,排序
From: https://www.cnblogs.com/eiffelzero/p/16864693.html

相关文章