forked from lolosssss/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189_rotate_array.c
More file actions
67 lines (57 loc) · 1.08 KB
/
189_rotate_array.c
File metadata and controls
67 lines (57 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Description : Rotate Array
* Rotate an array of n elements to the right by k steps.
* Author : Evan Lau
* Date : 2016/03/13
*/
#include <stdio.h>
void rotate(int* nums, int numsSize, int k)
{
int begin = 0;
int end = numsSize - 1;
int tmp;
if (k % numsSize == 0)
{
return;
}
k = k % numsSize;
while (begin < end)
{
tmp = nums[begin];
nums[begin] = nums[end];
nums[end] = tmp;
begin++;
end--;
}
begin = 0;
end = k - 1;
while (begin < end)
{
tmp = nums[begin];
nums[begin] = nums[end];
nums[end] = tmp;
begin++;
end--;
}
begin = k;
end = numsSize - 1;
while (begin < end)
{
tmp = nums[begin];
nums[begin] = nums[end];
nums[end] = tmp;
begin++;
end--;
}
}
int main(void)
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
rotate(arr, 10, 2);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}