-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathAC_pointer_n_1space.cpp
More file actions
43 lines (37 loc) · 854 Bytes
/
AC_pointer_n_1space.cpp
File metadata and controls
43 lines (37 loc) · 854 Bytes
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_pointer_n_1space.cpp
* Create Date: 2015-04-01 11:14:48
* Descripton: O(1) space
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
void reverse_array(int nums[], int a, int b) {
while (a < b) {
swap(nums[a], nums[b]);
++a;
--b;
}
}
public:
void rotate(int nums[], int n, int k) {
if (n == 0 || k % n == 0)
return;
k = k % n;
reverse_array(nums, 0, n - k - 1);
reverse_array(nums, n - k, n - 1);
reverse_array(nums, 0, n - 1);
}
};
int main() {
int num[] = {1, 2, 3, 4, 5};
Solution s;
s.rotate(num, 5, 3);
for (int i = 0; i < 5; ++i)
cout << num[i] << ' ';
cout << endl;
return 0;
}