We use two pointers \(i\) and \(j\), initially pointing to the start and end of the array respectively. Each time, we swap the elements at \(i\) and \(j\), then move \(i\) forward and \(j\) backward, until \(i\) and \(j\) meet.
The time complexity is \(O(n)\), where \(n\) is the length of the array. The space complexity is \(O(1)\).
/** Do not return anything, modify s in-place instead. */functionreverseString(s:string[]):void{for(leti=0,j=s.length-1;i<j;++i,--j){[s[i],s[j]]=[s[j],s[i]];}}
/** * @param {character[]} s * @return {void} Do not return anything, modify s in-place instead. */varreverseString=function(s){for(leti=0,j=s.length-1;i<j;++i,--j){[s[i],s[j]]=[s[j],s[i]];}};