Leetcode notes: Search in Rotated Sorted Array, leetcoderotated

Source: Internet
Author: User

Leetcode notes: Search in Rotated Sorted Array, leetcoderotated

I. Description

Ii. problem-solving skills

As this question rotates, elements smaller than the first element may appear in the second half of the value or do not appear. Therefore, we can consider using the binary search of variants, that is, before comparing the intermediate element with the target, we can first compare the relationship between the first element and the target. There are three situations at this time:

1. The first element is equal to the target, returns the coordinates of the first element, and the function ends;
2. If the first element is greater than the target, the target may be rotated to the end of the array. At this time, we also need to compare the relationship with the elements in the middle of the array. At this time, there are three situations:

A. the intermediate element is greater than the first element. At this time, the target may exist in the second half of the array. Call the function recursively to find the target coordinate. B. if the intermediate element is equal to the target, the coordinates of the intermediate element are returned, and the function ends. c. the intermediate element is smaller than the first element. At this time, it can be divided into two situations: (1 ). if the intermediate element is smaller than the target element, the target element may exist in the second half of the array and call the function recursively to find the target coordinate. (2 ). if the intermediate element is greater than the target element, the target element may exist in the First Half of the array and call the function recursively to find the target coordinate;

3. The first element is smaller than the target, and there are three situations to consider:

A. if the intermediate element is equal to the target element, the coordinates of the intermediate element are returned, and the function ends. B. the intermediate element is greater than the first element. At this time, there are two cases to consider: (1 ). if the intermediate element is greater than the target element, the target element may exist in the First Half of the array and call the function recursively to find the target coordinate. (2 ). if the intermediate element is smaller than the target element, the target element may exist in the second half of the array. recursively call the function to find the target coordinate. c. if the intermediate element is smaller than the first element, the target element may exist in the First Half of the array and call the function recursively to find the target coordinate;

Of course, you also need to consider the number of elements in the array as 0, 1, 2, as well as the starting position coordinates of the array and the number of elements in the array during recursion, these are the difficulties of this question. I have debugged the code for a long time.

Iii. Sample Code

// Time complexity O (log n), space complexity O (1) # include <iostream> using namespace std; class Solution {public: int SearchRotatedSortedArray (int A [], int n, int target) {int start = 0; int end = n; int middle = start + (end-start)/2; while (start! = End) {if (target = A [middle]) return middle; if (A [start] <A [middle]) {if (target <A [middle]) & (A [start] <= target) end = middle; else start = middle + 1;} else {if (target> A [middle]) & (target <= A [end-1]) start = middle + 1; else end = middle ;}} return-1; //-1} is returned if the target element cannot be found in the array }};

Iv. Experience

The difficulty lies in the pointer setting of the first element of the array in the boundary condition and Recursion process and the setting of the number of array elements. The boundary condition is often the focus of the interview questions.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.