C++實(shí)現(xiàn)LeetCode(80.有序數(shù)組中去除重復(fù)項(xiàng)之二)
[LeetCode] 80. Remove Duplicates from Sorted Array II 有序數(shù)組中去除重復(fù)項(xiàng)之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice 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.
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)過(guò)一次重復(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)。理清了上面的思路,則代碼很好寫(xiě)了:
解法一:
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)就可以了,參見(jiàn)代碼如下:
解法二:
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; } };
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(80.有序數(shù)組中去除重復(fù)項(xiàng)之二)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)有序數(shù)組中去除重復(fù)項(xiàng)之二內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何使用VSCode和CMake構(gòu)建跨平臺(tái)的C/C++開(kāi)發(fā)環(huán)境
本文主要介紹了如何使用VSCode和CMake構(gòu)建跨平臺(tái)的C/C++開(kāi)發(fā)環(huán)境,想進(jìn)行跨平臺(tái)開(kāi)發(fā)的同學(xué)們,一定要看一下2021-06-06C語(yǔ)言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn)
這篇文章主要介紹了C語(yǔ)言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07C++ Boost shared_ptr共享指針詳細(xì)講解
shared_ptr是一個(gè)標(biāo)準(zhǔn)的共享所有權(quán)的智能指針,允許多個(gè)指針指向同一個(gè)對(duì)象,定義在memory文件中,命名空間為std,這篇文章主要介紹了C++ shared_ptr使用,需要的朋友可以參考下2022-11-11kernel劫持modprobe?path內(nèi)容詳解
這篇文章主要為大家介紹了kernel劫持modprobe?path的內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05C++基礎(chǔ)入門(mén)教程(三):數(shù)組、字符串、結(jié)構(gòu)體、共用體
這篇文章主要介紹了C++基礎(chǔ)入門(mén)教程(三):數(shù)組、字符串、結(jié)構(gòu)體、共用體,需要的朋友可以參考下2014-11-11C++線程池的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了C++線程池的簡(jiǎn)單實(shí)現(xiàn)方法,包括了線程操作函數(shù)及相關(guān)屬性的用法,需要的朋友可以參考下2014-09-09C語(yǔ)言實(shí)現(xiàn)飛機(jī)售票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)飛機(jī)售票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05