My clear cpp code


  • 0
    C
    class Solution {
    public:
        string reverseString(string s) 
        {
            int i=0, j=s.size()-1;
            while(i < j)
                std::swap(s[i++], s[j--]);
            return s;
        }
    };

  • 0
    Y

    it can be more simple

       for(int i=0,j=s.size()-1;i<j;swap(s[i++], s[j--]));
        return s;
    

  • 1
    class Solution {
    public:
        string reverseString(string s) {
            return string(s.crbegin(),s.crend());
        }
    };

Log in to reply
 

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