Skip to content

344_ReverseString

a920604a edited this page Apr 14, 2023 · 1 revision

title: 344. Reverse String tags:
- Two Pointers categories: leetcode comments: false

solution

class Solution {
public:
    void reverseString(vector<char>& s) {
        reverse(s.begin(), s.end());
    }
};

option 1 - Two Pointers

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--]);
    }
};

option 2 - stack

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();}
    }
};

option 3 - without loop , using recursive

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);
       }
    }
};

analysis

  • option 1 - Two Pointers
    • time complexity O(n)
    • space complexity O(1)
  • option 2 - stack
    • time complexity O(n)
    • space complexity O(n)
Clone this wiki locally