• 23.8%

https://leetcode.com/problems/rotate-array/

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.

Hint:

Could you do it in-place with O(1) extra space?

Related problem: Reverse Words in a String II

https://discuss.leetcode.com/topic/9237/3-line-using-reverse

Oct 14， 2017

https://discuss.leetcode.com/topic/9406/3-lines-of-c-in-one-pass-using-swap

Every swap will put one number into its correct position, so the running time is O(n)

For example,

at first, nums[] is [1,2,3,4,5,6,7], n is 7, k is 3

after first outer loop, nums[] is [4,1,2,3], n is 4, k is 3

after second outer loop, nums[] is [4], n is 1, k is 0

loop ends.

https://discuss.leetcode.com/topic/9801/summary-of-c-solutions

1. Make an extra copy and then rotate.

Time complexity: O(n). Space complexity: O(n).

#### python

236ms, 3.89%, June.22th, 2016

https://leetcode.com/discuss/29657/my-simple-python-code

python内置方法