Lintcode Python Simple Class topic 39. Restoring a rotated sorted array

Source: Internet
Author: User
Tags array sort lintcode

Title Description:

Given a rotated sorted array, restore its sort in place.

Have you ever encountered this problem in a real interview? Yes

Description

What is a rotating array?

    • For example, the original array is [1,2,3,4], then its rotation array can be [1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]

Sample Example

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

challenges

Extra Space with O (1) and O (n) time complexity

labelArray Sort Array

Topic Analysis:

challenges

Extra Space with O (1) and O (n) time complexity

1. Do not create a new array, operate on the original array; 2. Loop through the array to get the first and last element comparison of the array sequentially, and if [0]>[-1], remove [0] and append to the end of the array until [0]<[-1]

Source:

Class solution: "" "    @param nums:the rotated sorted array    @return: Nothing    " "    def  Recoverrotatedsortedarray (self, nums):        # Write your code here        n = len (nums) for        i in range (n):            if nums[0] >= nums[-1]:                 tmp = nums[0]                nums.remove (nums[0])                nums.append (TMP)            else:                return

  

Lintcode Python Simple Class topic 39. Restoring a rotated sorted array

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.