-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path541.cpp
More file actions
38 lines (34 loc) · 1.12 KB
/
Copy path541.cpp
File metadata and controls
38 lines (34 loc) · 1.12 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
class Solution {
public:
string reverseStr(string s, int k) {
int n = s.size() / (2*k);
int left = s.size() % (2*k);
for(int i = 0; i < n; i++){
int startIndx = 2*k*i;
int endIndx = 2*k*i + k - 1;
for(int j = 0; j < k/2; j++){
char tmp = s[j + startIndx];
s[j + startIndx] = s[endIndx - j];
s[endIndx - j] = tmp;
}
}
if(left < k){
int startIndx = n * 2 * k;
int endIndx = s.size() - 1;
for(int j = 0; j < (endIndx - startIndx + 1)/2; j++){
char tmp = s[j + startIndx];
s[j + startIndx] = s[endIndx - j];
s[endIndx - j] = tmp;
}
}else{
int startIndx = n * 2 * k;
int endIndx = n * 2 * k + k - 1;
for(int j = 0; j < k/2; j++){
char tmp = s[j + startIndx];
s[j + startIndx] = s[endIndx - j];
s[endIndx - j] = tmp;
}
}
return s;
}
};