Skip to main content

反转字符串

题目

反转字符串, 注意只能修改该字符串本身.

题解

使用双指针, 开头和结尾两两交换, 每交换一次, left 和 right 往中间挤一次.

/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function (s) {
let left = 0
let right = s.length - 1
while (left < right) {
const temp = s[left]
s[left] = s[right]
s[right] = temp
left++
right--
}
}