LeetCode-33. Search in rotated Sorted Array

Source: Internet
Author: User

The requirement of the title is to find the target from a sorted array that might have been broken, and return the index of target. First notice the "possible" in this topic three words, which indicates that the problem given in the array may be broken, or may not be broken, so there should be the following three cases:


In the first case, when there is no break, the second case and the third case are the cases that may be broken.

This is the brush Leetcode has encountered since the first marked hard topic, so began to do when still very excited, after reading the topic just feel that the situation is very complex, but did not seriously analyze the possible break of the situation, which is to avoid. It seems that in the first few chapters of the introduction to the algorithm there is an example of the need for such a situation to discuss the situation of breaking, so later encountered discontinuous break class problems can be used in this similar way to paint analysis.

After reading the topic I found that the topic seems to be from the array to find a number ... Well... Since it is an array, it seems to be a linear search, so I tried ... The result is accept .... Good hard class problem ... Since this method is simply too simple, no code is posted ...

Of course, this topic uses linear search time complexity is O (n), and the topic also has a sort of conditions are not used, both sorting, but also from the array to find elements, do not try to find the two points simply sorry myself. The idea of using binary search is correct, but the key question is how to modify the binary search so that it can be taken care of in three situations that may occur. Here the large classification standard is compared to the size of left and mid, if left <= mid, then is the first case and the second case, continue to be able to update the position of Target 2 in the location of the two-point search to find both ends; If left > mid is the third case, This can continue to be categorized according to the target's relative position.

At the same time, to pay attention to a problem, in the binary search, the size of the condition judgment is not strictly less than or greater than, but will be appended to the equals sign.

public class solution {public int search (int[] nums, int target) {//extreme situation
        
        if (nums = = null) return-1;
        int l = 0;
        
        int r = nums.length-1;
            Binary search while (L <= r) {int mid = (L + r)/2;
            if (nums[mid] = = target) {return mid; }else if (Nums[l] <= Nums[mid]) {//target is in the left part of situation 2 if (Nums[l]
                <= target && target <= Nums[mid]) {r = mid-1;
                }else{L = mid + 1; }}else{//target is in the right part of situation 3 if (Nums[mid] <= targe
                T && Target <= Nums[r]) {L = mid + 1;
                }else{r = mid-1;
    }}} return-1; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.