C++ Simple 9ms Solution O(n)


  • 0
    B

    Simple 9ms C++ Solution: Get the length of the string, and append starting from the last character, onto a new string.

    class Solution {
    public:
        string reverseString(string s) {
            string r;
            int len = s.length();
            for (int i = 0; i < len; i++) {
                r.push_back(s[len-i-1]);
            }
            return r;
        }
    };
    

Log in to reply
 

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