-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189_Rotate Array.cpp
More file actions
40 lines (33 loc) · 863 Bytes
/
189_Rotate Array.cpp
File metadata and controls
40 lines (33 loc) · 863 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
#include <iostream>
#include <vector>
#include <string>
#include <set>
using namespace std;
class Solution {
public:
void rotate(vector<int>& nums, int k) {
if (nums.empty()) return;
k = k % nums.size();
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
}
};
int main(int argc, char* argv[]) {
std::vector<int> nums;
int num, k;
string str(argv[1]);
k = atoi(str.c_str());
for (int i = 2; i < argc; i++){
str = string(argv[i]);
if (isdigit(str[0])){ // isdigit must be a char, one digit
num = atoi(str.c_str());
nums.push_back(num);
}
}
Solution s;
s.rotate(nums, k);
for (int i = 0; i < nums.size(); i++)
std::cout << nums[i] << " ";
return 0;
}