免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
[Leetcode] Remove Element 刪除數(shù)組元素

Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

雙指針?lè)?/h2>

復(fù)雜度

時(shí)間 O(N) 空間 O(1)

思路

用一個(gè)指針記錄不含給定數(shù)字的數(shù)組邊界,另一個(gè)指針記錄當(dāng)前遍歷到的數(shù)組位置。只有不等于給定數(shù)字的數(shù),才會(huì)被拷貝到子數(shù)組的邊界上。

代碼

public class Solution { public int removeElement(int[] nums, int val) { int pos = 0; for(int i = 0; i < nums.length; i++){ // 只拷貝非給定數(shù)字的元素 if(nums[i] != val){ nums[pos] = nums[i];pos++;}} return pos;}}

http://www.thinksaas.cn/topics/0/395/395865.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
LeetCode 912.排序數(shù)組
LeetCode 4. 尋找兩個(gè)正序數(shù)組的中位數(shù)
LeetCode之Remove Element
LeetCode刷題 -- 雙指針篇 -- 三數(shù)之和
?LeetCode刷題實(shí)戰(zhàn)283:移動(dòng)零
刪除數(shù)組元素
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服