Simple C++ 9ms solution


  • 0
    class Solution {
    public:
        string reverseString(string s) {
            int head=0;
            int tail=s.size()-1;
            
            while(head<tail){
                char temp=s[head];
                s[head]=s[tail];
                s[tail]=temp;
                
                head++;
                tail--;
            }
            return s;
        }
    };
    

    Update(08/08/2017): Review and refine.

        string reverseString(string s) {
            reverse(s.begin(), s.end());
            return s;
        }
    

Log in to reply
 

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