|
1 | 1 | # -*- coding:utf-8 -*-
|
2 | 2 |
|
3 | 3 |
|
4 |
| -# Rotate an array of n elements to the right by k steps. |
| 4 | +# Given an array, rotate the array to the right by k steps, where k is non-negative. |
5 | 5 | #
|
6 |
| -# 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]. |
| 6 | +# Example 1: |
7 | 7 | #
|
8 |
| -# Note: |
9 |
| -# Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. |
10 | 8 | #
|
11 |
| -# [show hint] |
| 9 | +# Input: [1,2,3,4,5,6,7] and k = 3 |
| 10 | +# Output: [5,6,7,1,2,3,4] |
| 11 | +# Explanation: |
| 12 | +# rotate 1 steps to the right: [7,1,2,3,4,5,6] |
| 13 | +# rotate 2 steps to the right: [6,7,1,2,3,4,5] |
| 14 | +# rotate 3 steps to the right: [5,6,7,1,2,3,4] |
| 15 | +# |
| 16 | +# |
| 17 | +# Example 2: |
| 18 | +# |
| 19 | +# |
| 20 | +# Input: [-1,-100,3,99] and k = 2 |
| 21 | +# Output: [3,99,-1,-100] |
| 22 | +# Explanation: |
| 23 | +# rotate 1 steps to the right: [99,-1,-100,3] |
| 24 | +# rotate 2 steps to the right: [3,99,-1,-100] |
| 25 | +# |
| 26 | +# |
| 27 | +# Note: |
12 | 28 | #
|
13 |
| -# Hint: |
14 |
| -# Could you do it in-place with O(1) extra space? |
15 | 29 | #
|
16 |
| -# Related problem: Reverse Words in a String II |
| 30 | +# Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. |
| 31 | +# Could you do it in-place with O(1) extra space? |
17 | 32 | #
|
18 |
| -# Credits: |
19 |
| -# Special thanks to @Freezen for adding this problem and creating all test cases. |
20 | 33 | #
|
21 | 34 |
|
22 | 35 |
|
|
0 commit comments