题解 | #移除数组中的元素#

移除数组中的元素

https://www.nowcoder.com/practice/a93dd26ebb8c425d844acc17bcce9411

快慢指针
function removeWithoutCopy(arr, item) {
    let fast = 0
    let slow = 0
    while(fast < arr.length) {
        if(arr[fast] !== item) {
            arr[slow] = arr[fast]
            slow++
        }
        fast++
    }
    arr.length = slow
    return arr
}


全部评论

相关推荐

做黑夜里的那道光:两年电赛完赛没必要写,纯扣分
双非本科求职如何逆袭
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务