Data structure and algorithm-leetcode practice two-point search application

Source: Internet
Author: User

Leetcode 153. Find Minimum in rotated Sorted Array

Finds the smallest element of a looping sorted array (the circular sort array can be understood as a ring array (0,1,2,4,5,6,7), from which one element is broken after 4,5,6,7,0,1,2).

To find the smallest primitive of an array, we know that the simplest way is to iterate through the numbers, and the time complexity O (n) will definitely find this element. We look at the structure of the loop sort array, we find our array is not exactly a random ordinal group, we analyze the pattern of the array, we can see that our array is also partially ordered, our array can appear the following three kinds of situations:

    1. Left ordered (eg: 2,4,5,6,7,0,1)
    2. Right order (eg: 6,7,0,1,2,4,5)
    3. The whole order (eg: 0,1,2,4,5,6,7)

Analyzing the above, we know that our elements are at least partially or orderly, and we can try the dichotomy to find the smallest element, implemented as follows:

 Public Static int Findmin(int[] nums) {returnFindmin (Nums,0, Nums.length-1); }/ * * The implementation body of the binary lookup */     Public Static int Findmin(int[] arr,intLeftintright) {if(left = right) {returnArr[left]; }intMiddle = (left + right)/2;if(Arr[left] <= Arr[middle] && arr[right] <= Arr[middle]) {//When the element is left in an orderly condition            returnFindmin (arr, Middle +1, right); }Else if(Arr[left] >= Arr[middle] && arr[right] >= Arr[middle]) {//When element right is ordered            returnFindmin (arr, left, middle); }Else{//This element is all in order            returnArr[left]; }    }

Leetcode. Search in rotated Sorted Array
Finds an element that exists in a looping sorted array, returns the position (index) of the element, and returns 1 if the element does not exist.

 Public Static int findnums(int[] Nums,intTarget) {returnFindnums (Nums,0, Nums.length-1, target); }/ * * The implementation body of the binary lookup */     Public Static int findnums(int[] arr,intLeftintRightintTarget) {if(Left > right) {return-1; }intMiddle = (left + right)/2;if(Arr[left] = = target) {//The left element size of the current lookup section equals the target number            returnLeft }if(Arr[right] = = target)the right element size of the current lookup section equals the target number            returnRightif(Arr[middle] = = target) {the middle element size of the current lookup section equals the target number            returnMiddle; }if(Arr[left] < Arr[middle] && Arr[right] < Arr[middle]) {//When the element is left in an orderly condition            if(Arr[middle] > target) {//When the intermediate element is greater than the target element                //When the leftmost element is greater than the target element, look in the right half, otherwise, find in the left half                returnArr[left] > target? Findnums (arr, Middle +1, right, target): Findnums (arr, left, middle-1, target); }Else{//When the intermediate element is less than the target element                returnFindnums (arr, Middle +1, right, target); }        }Else if(Arr[left] > Arr[middle] && arr[right] > Arr[middle]) {//When element right is ordered            if(Arr[middle] > target) {//When the intermediate element is greater than the target element                returnFindnums (arr, left, middle-1, target); }Else{//When the intermediate element is less than the target element                //When the rightmost element is greater than the target element, look in the right half, otherwise, find in the left half                returnArr[right] > target? Findnums (arr, Middle +1, right, target): Findnums (arr, left, middle-1, target); }        }Else{//When the element is in an orderly condition            if(Arr[middle] > target) {returnFindnums (arr, left, middle-1, target); }Else{returnFindnums (arr, Middle +1, right, target); }        }    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure and algorithm-leetcode practice two-point search application

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.