/** * @param {character[]}s * @return {void}Do not return anything, modify s in-place instead. */ var reverseString = function (s) { s.reverse(); };
解法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * @param {character[]}s * @return {void}Do not return anything, modify s in-place instead. */ var reverseString = function (s) { let [left, right] = [0, s.length - 1];
while (left <= right) { const temp = s[left]; s[left] = s[right]; s[right] = temp; left ++; right --; } };