Skip to content

Commit d2872d2

Browse files
authored
Merge pull request #68 from swaroopgrs/patch-2
Update rotate-array.py
2 parents c35e1e0 + 687753f commit d2872d2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Python/rotate-array.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ def apply_cycle_permutation(self, k, offset, cycle_len, nums):
6060
nums[(offset + i * k) % len(nums)], tmp = tmp, nums[(offset + i * k) % len(nums)]
6161
nums[offset] = tmp
6262

63+
class Solution3:
64+
"""
65+
:type nums: List[int]
66+
:type k: int
67+
:rtype: void Do not return anything, modify nums in-place instead.
68+
"""
69+
70+
def rotate(self, nums, k):
71+
count = 0
72+
start = 0
73+
while count < len(nums):
74+
curr = start
75+
prev = nums[curr]
76+
while True:
77+
idx = (curr + k) % len(nums)
78+
nums[idx], prev = prev, nums[idx]
79+
curr = idx
80+
count += 1
81+
if start == curr:
82+
break
83+
start += 1
84+
6385

6486
if __name__ == '__main__':
6587
nums = [1, 2, 3, 4, 5, 6, 7]

0 commit comments

Comments
 (0)