Leetcode 344 - Reverse string

Note

  • Double pointers is the way to go.

Write a function that reverses a string. The input string is given as an array of characters s.

Example 1:

1
2
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let left = 0, right = s.length - 1;
while (left < right) {
[s[left], s[right]] = [s[right], s[left]];
left++;
right--;
}
return s;
};