L39 Recover Rotated Sorted Array
Given a rotated sorted array, recover it to sorted array in-place.
Clarification
What is rotated array?
For example, the orginal array is [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]
Example
[4, 5, 1, 2, 3]
->[1, 2, 3, 4, 5]
In-place, O(1) extra space and O(n) time.
这个可以用一个loop O(n)找到断点,然后3步翻转。不过我用了L159和L160的方法2分找,O(logn)。3步翻转用O(n),所以总体复杂度O(n)
Last updated
Was this helpful?