代码拉取完成,页面将自动刷新
同步操作将从 晴转阴/fe-handwriting 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// 反转字符串 https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnhbqj/
/**
*
*/
// 双指针
const reverseString = (s) => {
let i = 0
let j = s.length - 1
while (i < j) {
let temp = s[ i ]
s[ i ] = s[ j ]
console.log(i, j, temp, s[ j ], s[ i ])
s[ j ] = temp
i++
j--
}
return s
}
// 单指针,做对称交换
const reverseString2 = (s) => {
const len = s.length
const halfLen = len / 2
let i = 0
while (i < halfLen) {
const temp = s[ i ]
const tail = len - 1 - i
s[ i ] = s[ tail ]
s[ tail ] = temp
i++
}
return s
}
// 结构交换两个值
const reverseString3 = (s) => {
const len = s.length
for (let left = 0, right = len - 1; left < right; left++, right--) {
[ s[ right ], s[ left ] ] = [ s[ left ], s[ right ] ]
}
return s
}
console.log(reverseString(["h","e","l","l","o"]))
console.log(reverseString2(["h","e","l","l","o"]))
console.log(reverseString3(["h","e","l","l","o"]))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。