-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path344. Reverse String.cpp
44 lines (38 loc) · 1.01 KB
/
344. Reverse String.cpp
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
// Solution one
class Solution {
public:
void reverseString(vector<char>& s) {
rec(s, 0, s.size() - 1);
/*
reverse(s.begin(), s.end());
for(int i=0; i<s.size(); i++){
if(i == 0) cout << "[\"" << s[i] << '"' << ",";
else if(i == s.size()-1){
cout << '"' << s[i] << '"' << "]" << endl;
}
else cout << '"' << s[i] << '"' << ",";
}
*/
}
void rec(vector<char> &v, int st, int ed){
if(st >= ed){
return;
}
rec(v, st + 1, ed - 1);
swap(v[st], v[ed]);
}
};
// Solution Two
class Solution {
public:
void reverseString(vector<char>& s) {
reverse(s.begin(), s.end());
for(int i=0; i<s.size(); i++){
if(i == 0) cout << "[\"" << s[i] << '"' << ",";
else if(i == s.size()-1){
cout << '"' << s[i] << '"' << "]" << endl;
}
else cout << '"' << s[i] << '"' << ",";
}
}
};