Leetcode Search in rotated Sorted Array (C,c++,java,python)

Source: Internet
Author: User

Problem:

Suppose a sorted array is rotated on 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 is given a target value to search. If found in the array is return its index, otherwise return-1.

Assume no duplicate exists in the array.

Solution: The problem is a two-point search of the deformation, mainly to find the tipping point (the next value is smaller than it) on it, and then look at the target size to determine in which range to find the binary.
The main idea: to a well-ordered array, but was flipped over, is a part of the front is received by the array, now give a target value, ask for the value in the array subscript, no output-1
Java source Code (329MS):
public class Solution {public    int search (int[] nums, int target) {        int index=0,len=nums.length;        while (index<len-1 && nums[index]<=nums[index+1]) index++;        if (Target>=nums[0] && Target<=nums[index]) {            return find (Nums,0,index,target);        } else{            return Find (Nums,index+1,len-1,target);        }    }    private int Find (int[] nums,int start,int end,int target) {        if (start>end) return-1;        int l=start,r=end,mid;        while (l<=r) {            mid= (l+r)/2;            if (Nums[mid]==target) return mid;            else if (Target<nums[mid]) r=mid-1;            else l=mid+1;        }        return-1;}    }

C Language Source code (3MS):
int find (int* nums,int start,int end,int target) {    int l=start,r=end,mid;    if (start>end) return-1;    while (l<=r) {        mid= (l+r) >>1;        if (Nums[mid]==target) return mid;        else if (nums[mid]>target) r=mid-1;        else l=mid+1;    }    return-1;} int search (int* nums, int numssize, int target) {    int index=0;    while (Index<numssize-1 && nums[index]<=nums[index+1]) index++;    if (target >= nums[0] && Target<=nums[index]) {        return find (Nums,0,index,target);    } else{        return Find (Nums,index+1,numssize-1,target);}    }

C + + source code (6MS):
Class Solution {public:    int search (vector<int>& nums, int target) {        int index=0,len=nums.size ();        while (index<len-1 && nums[index]<=nums[index+1]) index++;        if (Target>=nums[0] && Target<=nums[index]) {            return find (Nums,0,index,target);        } else{            return Find (Nums,index+1,len-1,target);        }    } Private:    int Find (vector<int>& nums,int start,int end,int target) {        if (start>end) return-1;        int l=start,r=end,mid;        while (l<=r) {            mid= (l+r) >>1;            if (Nums[mid]==target) return mid;            else if (nums[mid]>target) r=mid-1;            else l=mid+1;        }        return-1;    }};

Python source code (64MS):
Class solution:    # @param {integer[]} nums    # @param {integer} target    # @return {integer}    def search (self, Nums, target):        Index=0;length=len (nums) while        index<length-1 and Nums[index]<nums[index+1]:index+=1        if target>=nums[0] and Target<=nums[index]:return self.find (nums,0,index,target)        Else:return Self.find (nums,index+1,length-1,target)    def find (self,nums,start,end,target):        if start>end:return-1;        L=start;r=end while        l<=r:            mid= (l+r)/2            if Nums[mid]==target:return mid            elif target>nums[ Mid]:l=mid+1            else:r=mid-1        return-1


Leetcode Search in rotated Sorted Array (C,c++,java,python)

Related Article

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.