# C++,3 lines, easy to understand

• ``````/*****
****author: cxq
****weibo: http://weibo.com/chenxq1992
****/
class Solution {
public:
void rotate(int nums[], int n, int k) {
k = k % n;
reverse(nums, nums + n);
reverse(nums, nums + k);
reverse(nums + k, nums + n);
}
};``````

• nice! Can you briefly explain the principle behind it?

• Well, it's simple. Firstly, we reverse the whole array,that makes the element(0, n-1) become element(n-1, 0).Secondly,we reverse element(0,k-1) to make sure the original order.Then
we reverse element(k, n-1) to make sure the original order,too.You can draw the procedure
on the paper, that will make it clear! :)

• can u explain the proble's meaning thanks

• it is array move, in the question example, n = 7 and k = 3 so it means[1,2,3,4,5,6,7] has 7 elements and you need to move each element 3 forward, so the result is is [5,6,7,1,2,3,4]. If k =1, the result is [7,1,2,3,4,5,6]

• Actually,it's so-called ROR! :)

• Is reverse a builtin function in C++? I don't quite get what the reverse function does

• It is a STL algorithm.

• This is so smart

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.