# Rotate Array 继续续

```void rotate(int nums[], int n, int k) {

}```

[1, 2, 3, 4, 5, 6] ==》 [5, 6, 1, 2, 3, 4]

[1, 2, 3, 4, 5, 6]

[4, 3, 2, 1, 6, 5]

[5, 6, 1, 2, 3, 4]

```void reverse(int nums[], int m, int n){
int tmp = 0;
while (m++ != n--){
tmp = nums[n];
nums[n] = nums[m];
nums[m] = tmp;
}
}

void rotate(int nums[], int n, int k) {
k = k % n;
reverse(nums, 0, n - k - 1);
reverse(nums, n - k, n - 1);
reverse(nums, 0, n - 1);
}```

```Submission Result: Runtime Error

Last executed input:	[1], 0```

```void reverse(int nums[], int m, int n){
int tmp = 0;
while (m++ < n--){
tmp = nums[n];
nums[n] = nums[m];
nums[m] = tmp;
}
}

void rotate(int nums[], int n, int k) {
k = k % n;
reverse(nums, 0, n - k - 1);
reverse(nums, n - k, n - 1);
reverse(nums, 0, n - 1);
}```

```Submission Result: Wrong Answer
Input:		[1,2,3], 1
Output:		[2,1,3]
Expected:	[3,1,2]```

```void reverse(int nums[], int m, int n){
int tmp = 0;
while (m < n){
tmp = nums[n];
nums[n] = nums[m];
nums[m] = tmp;
++m;
--n;
}
}

void rotate(int nums[], int n, int k) {
k = k % n;
reverse(nums, 0, n - k - 1);
reverse(nums, n - k, n - 1);
reverse(nums, 0, n - 1);
}```

```33 / 33 test cases passed.
Status: Accepted
Runtime: 11 ms```

[1, 2, 3, 4, 5, 6] ==》 [5, 6, 1, 2, 3, 4]

[1, 2, 3, 4, 5, 6]

[5, 1, 2, 3, 4, 6]

[5, 6, 1, 2, 3, 4]

```void rotate(int nums[], int n, int k) {
k = k % n;
int i;
int j;
int tmp = 0;
for (i = n - k; i < n; i++){
tmp = nums[i];
for (j = n - k - 1; j >= 0; j--)
nums[j + 1] = nums[j];
nums[j] = tmp;
}
}```

```Submission Result: Runtime Error
Last executed input:	[1,2], 1```

[1, 2, 3, 4, 5, 6]

[5, 1, 2, 3, 4, 6]

[5, 6, 1, 2, 3, 4]

nums[n-k] = 5，覆盖nums[0]

nums[n-k+1] = 6，覆盖nums[1]

```void rotate(int nums[], int n, int k) {
k = k % n;
int i;
int j;
int tmp = 0;
for (i = n - k; i < n; i++){
tmp = nums[i];
for (j = n - k - 1; j >= 0; j--)
nums[j + 1] = nums[j];
nums[i - n + k] = tmp;
}
}```

```Input:	[1,2,3], 2
Output:	[2,3,3]
Expected:	[2,3,1]```

```void rotate(int nums[], int n, int k) {
k = k % n;
int i;
int j;
int tmp = 0;
for (i = n - k; i < n; i++){
tmp = nums[i];
for (j = i - 1; j >= i - n + k; j--)
nums[j + 1] = nums[j];
nums[i - n + k] = tmp;
}
}```

```33 / 33 test cases passed.
Status: Accepted
Runtime: 354 ms```