344.反转字符串

题目:[https://leetcode-cn.com/problems/reverse-string/description/]
思路:容器的操作,反正给定string即可
代码:

class Solution
{
public:
    string reverseString(string s)
    {
        string obj;
        for (auto it=s.end(); !s.empty()&&it!=s.begin(); --it)  //empty为 空返回1,不不为空返回0
        {
            obj+=*(it-1);
        }
        return obj;
    }
};

讨论区范例代码:

class Solution
{
public:
    string reverseString(string s)
    {
        int i = 0, j = s.size() - 1;
        while(i < j)
        {
            swap(s[i++], s[j--]);
        }
        return s;
    }
};
posted @ 2018-11-03 20:36  Kipper  阅读(60)  评论(0)    收藏  举报