344 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 n = s.size();
        if ( n == 0 ) return s;
        int i = 0, j = n-1;
        while ( i <= j ) swap(s[i++], s[j--]);
        return s;
    }
};

results matching ""

    No results matching ""