Skip to main content

Leetcode - 344. Reverse String

· One min read

https://leetcode.com/problems/reverse-string/

Solution 1

/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function (s) {
if (s.length === 1) {
return s;
}

const middle = Math.floor(s.length / 2);

for (let i = 0; i < middle; i++) {
const a = s[i];
const b = s[s.length - i - 1];
s[i] = b;
s[s.length - i - 1] = a;
}

return s;
};