FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/array/rotate_array.py at master · MLucien/algorithms · GitHub
MLucien
/
algorithms
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms
/
array
/
rotate_array.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (24 loc) · 667 Bytes
Breadcrumbs
algorithms
/
array
/
rotate_array.py
Copy path
File metadata and controls
30 lines (24 loc) · 667 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3,
the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can,
there are at least 3 different ways to solve this problem.
"""
def
rotate
(
nums
,
k
):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
n
=
len
(
nums
)
k
=
k
%
n
reverse
(
nums
,
0
,
n
-
k
-
1
)
reverse
(
nums
,
n
-
k
,
n
-
1
)
reverse
(
nums
,
0
,
n
-
1
)
def
reverse
(
array
,
a
,
b
):
while
a
<
b
:
array
[
a
],
array
[
b
]
=
array
[
b
],
array
[
a
]
a
+=
1
b
-=
1
Back
|
FazBrowse Home
|
New Git URL