這篇文章主要講解了“C++有序數(shù)組中去除重復(fù)項(xiàng)的方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C++有序數(shù)組中去除重復(fù)項(xiàng)的方法”吧!
成都創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元懷遠(yuǎn)做網(wǎng)站,已為上家服務(wù),為懷遠(yuǎn)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
Example 1:
Given nums = [1,1,1,2,2,3],
Your function should return length =
5
, with the first five elements of
nums
being
1, 1, 2, 2
and 3 respectively.
It doesn"t matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,1,2,3,3],
Your function should return length =
7
, with the first seven elements of
nums
being modified to
0
, 0, 1, 1, 2, 3 and 3 respectively.
It doesn"t matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
這道題是之前那道 Remove Duplicates from Sorted Array 的拓展,這里允許最多重復(fù)的次數(shù)是兩次,那么可以用一個(gè)變量 cnt 來(lái)記錄還允許有幾次重復(fù),cnt 初始化為1,如果出現(xiàn)過一次重復(fù),則 cnt 遞減1,那么下次再出現(xiàn)重復(fù),快指針直接前進(jìn)一步,如果這時(shí)候不是重復(fù)的,則 cnt 恢復(fù)1,由于整個(gè)數(shù)組是有序的,所以一旦出現(xiàn)不重復(fù)的數(shù),則一定比這個(gè)數(shù)大,此數(shù)之后不會(huì)再有重復(fù)項(xiàng)。理清了上面的思路,則代碼很好寫了:
解法一:
class Solution { public: int removeDuplicates(vector<int>& nums) { int pre = 0, cur = 1, cnt = 1, n = nums.size(); while (cur < n) { if (nums[pre] == nums[cur] && cnt == 0) ++cur; else { if (nums[pre] == nums[cur]) --cnt; else cnt = 1; nums[++pre] = nums[cur++]; } } return nums.empty() ? 0 : pre + 1; } };
這里其實(shí)也可以用類似于 Remove Duplicates from Sorted Array 中的解法三的模版,由于這里最多允許兩次重復(fù),那么當(dāng)前的數(shù)字 num 只要跟上上個(gè)覆蓋位置的數(shù)字 nusm[i-2] 比較,若 num 較大,則絕不會(huì)出現(xiàn)第三個(gè)重復(fù)數(shù)字(前提是數(shù)組是有序的),這樣的話根本不需要管 nums[i-1] 是否重復(fù),只要將重復(fù)個(gè)數(shù)控制在2個(gè)以內(nèi)就可以了,參見代碼如下:
解法二:
class Solution { public: int removeDuplicates(vector<int>& nums) { int i = 0; for (int num : nums) { if (i < 2 || num > nums[i - 2]) { nums[i++] = num; } } return i; } };
感謝各位的閱讀,以上就是“C++有序數(shù)組中去除重復(fù)項(xiàng)的方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)C++有序數(shù)組中去除重復(fù)項(xiàng)的方法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
新聞名稱:C++有序數(shù)組中去除重復(fù)項(xiàng)的方法
標(biāo)題網(wǎng)址:http://aaarwkj.com/article44/gjoshe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、品牌網(wǎng)站制作、Google、App設(shè)計(jì)、企業(yè)建站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)