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

Example:
Given s = “hello”, return “olleh”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
string reverseString(string s) {
int l = s.length(), left = 0, right = l - 1;
if (l < 2)
return s;
while (left < right) {
swap(s[left++], s[right--]);
}
return s;
}

void swap(char &a, char &b) {
char tmp = a;
a = b;
b = tmp;
}
};