151. Reverse Words in a String

202410212359
tags: #string #two-pointers

var reverseWords = function(s) {
	const sArr = s.split(' ').filter(word => word !== "");
	let start = 0;
	let end = sArr.length - 1;
	while (start < end) {
		[sArr[start], sArr[end]] = [sArr[end], sArr[start]];
		start += 1;
		end -= 1;
	}
	return sArr.join(' ');
};

Reference

https://leetcode.com/problems/reverse-words-in-a-string/