[LeetCode] Reverse String

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

Example:
Given s = "hello", return "olleh".

题目要求反转一个字符串,利用双指针求解。

class Solution {
public:
    string reverseString(string s) {
        int left = 0, right = s.size() - 1;
        while (left <= right)
            swap(s[left++], s[right--]);
        return s;
    }
};
// 9 ms

 相关题目:Reverse String II

posted @ 2017-07-10 09:59  immjc  阅读(115)  评论(0编辑  收藏  举报