344. Reverse String

Leetcode

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

題目

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

解答

  • 方法一

var reverseString = function(s) {
    let temp;
    for(let i=0; i<s.length/2; i++) {
        const j = s.length-i-1;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
};

Runtime: 112 ms, faster than 56.38% of JavaScript online submissions for Reverse String.

Memory Usage: 45.4 MB, less than 94.78% of JavaScript online submissions for Reverse String

測資

let s = ["h","e","l","l","o"];

reverseString(s);
console.log(s);

Last updated

Was this helpful?