Skip to content

Commit

Permalink
Rotate Array
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Oct 2, 2018
1 parent 4b6f432 commit 9e4944e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lc189. Rotate Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:

def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
def reserve(start, end):
for i in range((end - start) // 2):
nums[i + start], nums[end - i -
1] = nums[end - i - 1], nums[i + start]

length = len(nums)
k %= length
reserve(length - k, length)
reserve(0, length - k)
reserve(0, length)


nums = [1,2,3,4,5,6,7]
k = 3
s = Solution()
s.rotate(nums, k)
print(nums)

0 comments on commit 9e4944e

Please sign in to comment.