LintCode Q39 Recover Rotated Sorted Array in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2017年1月25日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
"""
@param nums: The rotated sorted array
@return: nothing
"""
def recoverRotatedSortedArray(self, nums):
# write your code here
# time complexity: O(n)
# space complexity: O(1)
rotated = False
for i in xrange(1, len(nums)):
if nums[i-1] > nums[i]:
rotated = True
break
if not rotated:
return
while i > 0:
temp = nums[0]
for j in xrange(1, len(nums)):
nums[j - 1] = nums[j]
nums[-1] = temp
i -= 1
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。