Simple c++ solution (no swap)


  • 0
    class Solution {
    public:
        string reverseString(string s) {
            string reversedString = "";
            for(int i = s.length() - 1; i >= 0; i--){
                reversedString += s.at(i);
            }
            return reversedString;
        }
    };
    

  • 0
    P

    @space_sailor That is essentially what I was trying to do, but for some reason my answer is "".

    class Solution {
    public:
        string reverseString(string s) {
            string z = "";
            string::iterator it;
            
            for (it = s.end(); it >= s.begin(); it--) {
                z += *it;
            }
            cout << "z = " << z << ", Size = " << z.size() << endl;
    
            return z;
        }
    };
    

    However, my stdout is correct.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.