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