344. Reverse String

Write a function that takes a string as input and returns the string reversed.

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

 

posted @ 2017-06-12 15:13  NaiveCoder  阅读(97)  评论(0)    收藏  举报