CyclicRotation

problem

solution

option 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

vector<int> solution(vector<int> &A, int K) {
// write your code in C++14 (g++ 6.2.0)
if(A.empty()) return A;
int n = A.size();
K %=n;
vector<int> ret(n,0);
for(int i=0;i<n;++i) ret[i] = A[(n-K+i)%n];
return ret;
}

option 2 - reverse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include<algorithm>
vector<int> solution(vector<int> &A, int K) {
// write your code in C++14 (g++ 6.2.0)

if(A.empty()) return A;
int n = A.size();
K %=n;
reverse(A.begin(), A.begin()+n-K);
reverse(A.begin()+n-K, A.end());
reverse(A.begin(), A.end());
return A;
}

analysis

  • time complexity O(n)
  • space complexity O(1)