Find Minimum in rotated Sorted array (smallest number of rotated arrays)

Source: Internet
Author: User

Title Description:

 is 0 1 2 4 5 6 7 4 5 6 7 0 1 2   in the array.

This question "Jian refers to offer" on the original question, directly on the code

Solution:

intFindmin (vector<int>&nums) {    intStart =0; intEnd = Nums.size ()-1;  while(Start <end) {        if(Nums[start] <Nums[end]) Break; intMID = start + (End-start)/2; if(Nums[mid] >=Nums[start]) Start= Mid +1; ElseEnd=mid; }    returnNums[start];}

Reference:https://leetcode.com/discuss/13389/compact-and-clean-c-solution

The above procedure does not take into account the existence of the same element in the array. If considered, the code needs to be modified.

Solution:

intFindmin (vector<int>&nums) {    intStart =0; intEnd = Nums.size ()-1;  while(Start <end) {        if(Nums[start] <Nums[end]) Break; intMID = start + (End-start)/2; if(Nums[mid] >Nums[end]) Start= Mid +1; Else if(Nums[mid] <Nums[end]) End=mid; Else        {            ++start; --end; }    }    returnNums[start];}

Finally, attach a solution to the offer of swords:

intMininorder (vector<int> &nums,intStartintend) {    intMin =Nums[start];  for(inti =1; I < nums.size (); ++i) {if(Nums[i] <min) min=Nums[i]; }    returnmin;}intFindmin (vector<int>&nums) {    intStart =0; intEnd = Nums.size ()-1;  while(Start <end) {        if(Nums[start] <Nums[end]) Break; intMID = start + (End-start)/2; if(Nums[mid] = = Nums[start] && Nums[mid] = =Nums[end]) {            returnMininorder (nums, start, end); }        if(Nums[mid] >=Nums[start]) Start= Mid +1; ElseEnd=mid; }    returnNums[start];}

Ps:

The solution is not necessarily the best in the "Sword Point offer"!!!

Find Minimum in rotated Sorted array (smallest number of rotated arrays)

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.