Reverse Words in a String II
Description
null
Solution(javascript)
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
const reverseWords = function (s) {
let start = null
let end = null
const reverse = (i, j) => {
while (i < j) {
const temp = s[i]
s[i] = s[j]
s[j] = temp
i++
j--
}
}
for (let i = 0; i < s.length; i++) {
if (s[i] !== ' ' && start === null) {
start = i
}
if (s[i + 1] === ' ' || s[i + 1] === undefined) {
end = i
reverse(start, end)
start = null
end = null
}
}
return s.reverse()
}