mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 08:17:01 +00:00
Added rotate_array.py
This commit is contained in:
parent
90b278080c
commit
610f42396e
|
@ -58,9 +58,18 @@ def rotate_array_n_space(nums: list[int], k: int) -> list[int]:
|
|||
>>> rotate_array_n_space([8, -7, 22, -6], 9)
|
||||
[-6, 8, -7, 22]
|
||||
"""
|
||||
|
||||
length = len(nums)
|
||||
k = k % length
|
||||
return nums[-k:] + nums[: length - k]
|
||||
new_nums = []
|
||||
|
||||
for i in range(length - k, length):
|
||||
new_nums.append(nums[i])
|
||||
|
||||
for i in range(length - k):
|
||||
new_nums.append(nums[i])
|
||||
|
||||
return new_nums
|
||||
|
||||
|
||||
# Time: O(n) Space: O(k)
|
||||
|
|
Loading…
Reference in New Issue
Block a user