LeetCode Search in Rotated Sorted Array

       Today, I will share with you a more interesting question. What is the purpose of analyzing this question? Yes~ I hope everyone can flexibly use what they have learned to solve the problem. Take a look at the binary search of this question or the binary search you are familiar with. Well, of course it is, don't be fooled by the title~

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

       This question asks to search for a given value in a rotated array and return the coordinate if it exists, or -1 if it doesn't. We still consider the binary search method, but the difficulty of this question is that we don't know where the original array is rotated. We still use the example given in the title to analyze. For the array [0 1 2 4 5 6 7], there are the following seven types Rotation method:

                                                            0  1  2   4  5  6  7

                                                            7  0  1   2  4  5  6

                                                            6  7  0   1  2  4  5

                                                            5  6  7   0  1  2  4

                                                           4  5  6  7  0  1  2

                                                           2  4  5  6  7  0  1

                                                           1  2  4  5  6  7  0

       The key to the binary search method is to determine whether the left half or the right half is to be searched after the middle number is obtained. We observe that the red numbers above are in ascending order. From this, we can observe the law. If the middle number is smaller than the rightmost number If the number in the middle is greater than the rightmost number, the left half is in order. We only need to use the first and last two arrays in the ordered half to determine whether the target value is here. In an area, this way you can determine which half of the side to keep, the code is as follows:

package com.gaoxue.LeetCode;

public class RotatedSortedArray {

	public int search(int[] A, int target) {
	    if (A.length == 0) return -1;
	    int L = 0, R = A.length-1;
	    if (target < A[L] && target > A[R]) return -1;
	    //First determine which part is ordered, and then perform a binary search on the ordered array to determine which half of the target remains.
	    while (L <= R) {
	        int M = (L + R)/2;
	        if(A[M]==target)
	        	return M;
	        else if (A[M] <= A[R]) {
	            if (target > A[M] && target <= A[R]) {
	                L = M+1;
	            } else {
	                R = M-1;
	            }
	        } else {
	            if (target < A[M] && target >= A[L]) {
	                R = M-1;
	            } else {
	                L = M+1;
	            }
	        }
	    }
	    return -1;
	}
	public static void main(String[] args) {
		System.out.println(new RotatedSortedArray().search(new int[] {4,5,6,7,0,1,2}, 0));
	}
}

       You are not unfamiliar with such binary search, so what about the following implementation...

package com.gaoxue.LeetCode;

public class RotatedSortedArray {

	public int search(int[] A, int target) {
	    if (A.length == 0) return -1;
	    int L = 0, R = A.length-1;
	    if (target < A[L] && target > A[R]) return -1;
	    while (L < R) {
	        int M = (L + R)/2;
	        if (A[M] <= A[R]) {
	            if (target > A[M] && target <= A[R]) {
	                L = M+1;
	            } else {
	                R = M;
	            }            
	        } else {
	            if (target <= A[M] && target >= A[L]) {
	                    R = M;
	            } else {
	                L = M+1;
	            }
	        }
	    }
	    if (A[L] == target) return L;
	    else return -1;
	}
	public static void main(String[] args) {
		System.out.println(new RotatedSortedArray().search(new int[] {4,5,6,7,0,1,2}, 0));
	}
}

       Or the binary search you are familiar with, but it may not be easy for most people to do this problem, the reason is that the understanding of the essence of the binary search idea is not deep enough, how to understand a knowledge, first of all natural It is not to be confused by its changes, and the second is to be able to apply it proficiently, so let's start with the knowledge of binary search for this question~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324785131&siteId=291194637