當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
js splice方法_我用JS刷LeetCode | Day 8
生活随笔
收集整理的這篇文章主要介紹了
js splice方法_我用JS刷LeetCode | Day 8
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如有興趣,微信搜索「九零后重慶崽兒」,我們一起學前端。
刪除排序數組中的重復項:
說明:現階段的解題暫未考慮復雜度問題首發地址:
我用JS刷LeetCode | Day 8?www.brandhuang.comQuestion:
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
中文題目:
給你一個數組 nums 和一個值 val,你需要 原地 移除所有數值等于 val 的元素,并返回移除后數組的新長度。
不要使用額外的數組空間,你必須僅使用 O(1) 額外空間并 原地 修改輸入數組。
元素的順序可以改變。你不需要考慮數組中超出新長度后面的元素。
Example:
給定 nums = [3,2,2,3], val = 3,函數應該返回新的長度 2, 并且 nums 中的前兩個元素均為 2。你不需要考慮數組中超出新長度后面的元素。給定 nums = [0,1,2,2,3,0,4,2], val = 2,函數應該返回新的長度 5, 并且 nums 中的前五個元素為 0, 1, 3, 0, 4。注意這五個元素可為任意順序。你不需要考慮數組中超出新長度后面的元素。個人分析:
Answer:
var removeElement = function(nums, val) {for (let i = 0; i< nums.length; i++) {if (nums[i] == val) {nums.splice(i, 1)i--}}return nums.length };其他:
本題更多 JavaScript 解析,點擊鏈接訪問對應的答案:https://leetcode.com
總結
以上是生活随笔為你收集整理的js splice方法_我用JS刷LeetCode | Day 8的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java能写复杂的查询么_spring-
- 下一篇: React简介、虚拟DOM、Diff算法