-
Notifications
You must be signed in to change notification settings - Fork 0
344_ReverseString
a920604a edited this page Apr 14, 2023
·
1 revision
class Solution {
public:
void reverseString(vector<char>& s) {
reverse(s.begin(), s.end());
}
};
class Solution {
public:
void swap(char *a, char* b){
*a = *a^*b;
*b = *a^*b;
*a = *a^*b;
}
void reverseString(vector<char>& s) {
int l =0, r=s.size()-1;
while(l<r) swap(&s[l++], &s[r--]);
}
};
class Solution {
public:
void reverseString(vector<char>& s) {
stack<char> sta;
for(char c:s) sta.push(c);
for(int i=0;i<s.size() ; ++i) {s[i] = sta.top();sta.pop();}
}
};
class Solution {
public:
void reverseString(vector<char>& s) {
if(s.size() != 1){
char temp = s[0];
s.erase(s.begin());
reverseString(s);
s.push_back(temp);
}
}
};
- option 1 - Two Pointers
- time complexity
O(n)
- space complexity
O(1)
- time complexity
- option 2 - stack
- time complexity
O(n)
- space complexity
O(n)
- time complexity
footer