# 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.

1 2 3 4 5 6    k=2

5 6    和1234这两份，只是把1 2 3 4放在了前面

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int temp;
int slength=nums.size();
k=k%slength;
int start=0;
int end=slength-1;
while(start<end)
{
swap(nums[start],nums[end]);
start++;
end--;
}
start=0;
end=k-1;
while(start<end)
{
swap(nums[start],nums[end]);
start++;
end--;
}
start=k;
end=slength-1;
while(start<end)
{
swap(nums[start],nums[end]);
start++;
end--;
}

}
};

#### 189. Rotate Array [easy] (Python)

2016-07-28 10:32:27

#### LeetCode189——Rotate Array两种解法（一种易读，一种高效）

2015-02-26 09:54:19

#### Leetcode：189. Rotate Array（JAVA）

2016-03-21 16:31:54

#### LeetCode 189 Rotate Array（旋转数组）

2016-01-28 15:10:38

#### Rotate Array数组旋转

2016-08-17 21:37:05

#### Leetcode 189: Rotate Array

2015-03-06 07:51:14

#### 【LeetCode】Rotate Array 旋转数组

2015-04-23 15:38:43

#### LeetCode(189) Rotate Array

2015-03-20 12:24:02

#### 1334. Rotate Array

2018-06-17 17:24:54

#### 189. Rotate Array (python)

2016-10-30 16:09:17