344. Reverse String

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

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

反转字符串,逼我不用 reverse

class Solution {
public:
    string reverseString(string s) {
        int n = s.length();
        int l = 0, r = n - 1;
        while(l < r) {
            swap(s[l++],s[r--]);
        }
        return s;
    }
};

 

posted on 2017-07-27 22:02  Beserious  阅读(84)  评论(0编辑  收藏  举报