LeetCode 344. 反转字符串

Posted by cody1991 on August 9, 2020

简单思路:简单的反转一下

344. 反转字符串

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) {
  const len = s.length;
  for (let i = 0; i < len / 2; i++) {
    [s[i], s[len - 1 - i]] = [s[len - 1 - i], s[i]];
  }
  return s;
};

console.log(reverseString(["h", "e", "l", "l"]));