C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng))
[LeetCode] 26. Remove Duplicates from Sorted Array 有序數(shù)組中去除重復(fù)項(xiàng)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once 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,2],
Your function should return length =
2
, with the first two elements of
nums
being
1
and
2
respectively.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length =
5
, with the first five elements of
nums
being modified to
0
,
1
,
2
,
3
, and
4
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]);
}
這道題要我們從有序數(shù)組中去除重復(fù)項(xiàng),和之前那道 Remove Duplicates from Sorted List 的題很類似,但是要簡單一些,因?yàn)楫吘箶?shù)組的值可以通過下標(biāo)直接訪問,而鏈表不行。那么這道題的解題思路是使用快慢指針來記錄遍歷的坐標(biāo),最開始時(shí)兩個(gè)指針都指向第一個(gè)數(shù)字,如果兩個(gè)指針指的數(shù)字相同,則快指針向前走一步,如果不同,則兩個(gè)指針都向前走一步,這樣當(dāng)快指針走完整個(gè)數(shù)組后,慢指針當(dāng)前的坐標(biāo)加1就是數(shù)組中不同數(shù)字的個(gè)數(shù),代碼如下:
解法一:
class Solution { public: int removeDuplicates(vector<int>& nums) { int pre = 0, cur = 0, n = nums.size(); while (cur < n) { if (nums[pre] == nums[cur]) ++cur; else nums[++pre] = nums[cur++]; } return nums.empty() ? 0 : (pre + 1); } };
我們也可以用 for 循環(huán)來寫,這里的j就是上面解法中的 pre,i就是 cur,所以本質(zhì)上都是一樣的,參見代碼如下:
解法二:
class Solution { public: int removeDuplicates(vector<int>& nums) { int j = 0, n = nums.size(); for (int i = 0; i < n; ++i) { if (nums[i] != nums[j]) nums[++j] = nums[i]; } return nums.empty() ? 0 : (j + 1); } };
這里也可以換一種寫法,用變量i表示當(dāng)前覆蓋到到位置,由于不能有重復(fù)數(shù)字,則只需要用當(dāng)前數(shù)字 num 跟上一個(gè)覆蓋到到數(shù)字 nums[i-1] 做個(gè)比較,只要 num 大,則一定不會有重復(fù)(前提是數(shù)組必須有序),參見代碼如下:
解法三:
class Solution { public: int removeDuplicates(vector<int>& nums) { int i = 0; for (int num : nums) { if (i < 1 || num > nums[i - 1]) { nums[i++] = num; } } return i; } };
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng))的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)有序數(shù)組中去除重復(fù)項(xiàng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
opencv3機(jī)器學(xué)習(xí)之EM算法示例詳解
這篇文章主要介紹了opencv3機(jī)器學(xué)習(xí)之EM算法的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06C++實(shí)現(xiàn)基于時(shí)序公平的讀寫鎖詳解
讀寫鎖與普通的互斥鎖的區(qū)別在于有兩種上鎖方式:讀鎖和寫鎖,不用的用戶對同一個(gè)讀寫鎖獲取讀鎖是非互斥的,其他情況則是互斥的,本文小編將給大家詳細(xì)介紹C++實(shí)現(xiàn)基于時(shí)序公平的讀寫鎖,需要的朋友可以參考下2023-10-10C++實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(Map實(shí)現(xiàn))
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C++中四種對象生存期和作用域以及static的用法總結(jié)分析
以下是對C++中四種對象生存期和作用域以及static的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下2013-09-09