Recover Rotated Sorted Array

Source: Internet
Author: User

Given a rotated sorted array, recover it to sorted array in-place.

Has you met this question in a real interview?

YesWhich company asked do you question?AirbnbAlibabaAmazonAppleBaiduBloombergCiscoDropboxEbayFacebook Google Hulu Intel Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits Thanks for your feedback. Example

[4, 5, 1, 2, 3]-[1, 2, 3, 4, 5]

Challenge

In-place, O (1) extra space and O (n) time.

Clarification

What is rotated array?

    • For example, the orginal array are [1,2,3,4], the rotated array of it can be [1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]

1      Public voidRecoverrotatedsortedarray (arraylist<integer>nums)2     {3         //Write your code4         if(Nums.size () ==0| | Nums.size () ==1)return;5         intI =0;6          for(i=0; i<nums.size () -1;i++)7         {8             if(Nums.get (i) >nums.get (i+1)) Break;9         }Ten         if(i = = Nums.size ()-1)return; OneReverse (nums,0, i); AReverse (Nums,i+1,nums.size ()-1); -Reverse (Nums,0,nums.size ()-1); -         return; the          -          -     } -      +      -      Public voidReverse (arraylist<integer> nums,intLeftintRight ) +     { A          while(left<Right ) at         { -             inttemp =Nums.get (left); - Nums.set (Left,nums.get (right)); - Nums.set (right,temp); -left++; -right--; in          -         } to        +         return; -}

Recover Rotated Sorted Array

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.