Topic:
Write a function that reverses the input string. The input string is given in the form of the character array char[].
Instead of allocating extra space to another array, you must modify the input array in place, using O(1) extra space to solve the problem.
You can assume that all the characters in the array are printable characters in the ASCII code table.
Example:
- Example 1:
Input:"h"."e"."l"."l"."o"]
Output:"o"."l"."l"."e"."h"]
Copy the code
- Example 2:
Input:"H"."a"."n"."n"."a"."h"]
Output:"h"."a"."n"."n"."a"."H"]
Copy the code
The topic
Train of thought
Reverse array: Reverse method implemented in javascript
Loop through half the length of the array, replacing the specified element from front to back (with destruct assignment)
/ * *
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
* /
var reverseString = function(s) {
let len = s.length
for (let i = 0; i < parseInt(len / 2.10); i++) {
[s[i], s[len - i - 1]] = [s[len - i - 1], s[i]]
}
};
Copy the code
Public number: front end little bookboy
Every day a daily problem, write the solution of the problem will be updated to the public account one day a big Lee column welcome to pay attention to the message