Leetcode-033-search rotation sort array

Time:2021-11-23

Search rotation sort array

The integer array nums is arranged in ascending order, and the values in the array are different from each other.

Before passing to the function, Num rotates on a previously unknown subscript k (0 < = k < num.length), making the array [num [k], Num [K + 1],…, Num [n-1], Num [0], Num [1],…, Num [k-1]] (subscripts count from 0). For example, [0,1,2,4,5,6,7] may become [4,5,6,7,0,1,2] after rotation at subscript 3.

Give you the rotated array num and an integer target. If the target value target exists in num, return its subscript, otherwise return – 1.

See leetcode’s official website for an example.

Source: leetcode
Link: https://leetcode-cn.com/probl…
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Solution 1: binary search

First, if num has only one number, directly judge whether the number is equal to target and return the result;

If nums has more than one bit, first traverse through nums to obtain the maximum position maxindx, and then there are two cases:

  • Judge that if the target is not greater than the last digit of num, use the binary search method to find the number in num(maxIndx, nums.length - 1)Whether there is an element equal to the target value in the. If so, the corresponding position is returned. If not, – 1 is returned;
  • If the target is greater than the number of the last digit of num, the binary search method is used to find the number in num(0, maxIndx)Whether there is an element equal to the target value in the. If so, the corresponding position is returned. If not, – 1 is returned.
public class LeetCode_033 {
    public static int search(int[] nums, int target) {
        if (nums.length == 1) {
            if (nums[0] == target) {
                return 0;
            } else {
                return -1;
            }
        }
        //Location of maximum value
        int maxIndx = -1;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                maxIndx = i;
                break;
            }
        }
        if (target <= nums[nums.length - 1]) {
            return find(nums, maxIndx + 1, nums.length - 1, target);
        } else {
            return find(nums, 0, maxIndx, target);
        }
    }

    /**
     *Binary search
     * @param nums
     * @param left
     * @param right
     * @param target
     * @return
     */
    public static int find(int[] nums, int left, int right, int target) {
        int mid;
        while (left <= right) {
            mid = (left + right) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{4, 5, 6, 7, 0, 1, 2};
        System.out.println(search(nums, 0));
    }
}

[daily message]May every waking day be accompanied by sunshine, perhaps in the clear sky, perhaps in my heart.

Recommended Today

Application of observer pattern in design pattern in Ruby Programming

Observer mode(sometimes referred to as publish / subscribe mode) is softwareDesign patternA kind of.In this mode, a target object manages all its dependent observer objects and actively notifies when its own state changes.This is usually achieved by calling the methods provided by each observer. When implementing the observer mode, it should be noted that the […]