Skip to content

Commit

Permalink
Merge pull request keon#88 from ankit167/Rotate_array_one_by_one
Browse files Browse the repository at this point in the history
Rotate array one by one
  • Loading branch information
keon authored Sep 13, 2017
2 parents 5627013 + 1a5ef78 commit 2910351
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions array/rotate_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
"""


#
# Rotate the entire array 'k' times
# T(n)- O(nk)
#
def rotate_one_by_one(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
n = len(nums)
for i in range(k):
temp = nums[n-1]
for j in range(n-1, 0, -1):
nums[j] = nums[j-1]
nums[0] = temp


#
# Reverse segments of the array, followed by the entire array
# T(n)- O(n)
#
def rotate(nums, k):
"""
:type nums: List[int]
Expand Down

0 comments on commit 2910351

Please sign in to comment.