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 的題很類(lèi)似,但是要簡(jiǎn)單一些,因?yàn)楫吘箶?shù)組的值可以通過(guò)下標(biāo)直接訪(fǎng)問(wèn),而鏈表不行。那么這道題的解題思路是使用快慢指針來(lái)記錄遍歷的坐標(biāo),最開(kāi)始時(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)來(lái)寫(xiě),這里的j就是上面解法中的 pre,i就是 cur,所以本質(zhì)上都是一樣的,參見(jiàn)代碼如下:
解法二:
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);
}
};
這里也可以換一種寫(xiě)法,用變量i表示當(dāng)前覆蓋到到位置,由于不能有重復(fù)數(shù)字,則只需要用當(dāng)前數(shù)字 num 跟上一個(gè)覆蓋到到數(shù)字 nums[i-1] 做個(gè)比較,只要 num 大,則一定不會(huì)有重復(fù)(前提是數(shù)組必須有序),參見(jiàn)代碼如下:
解法三:
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
判斷指定的進(jìn)程或程序是否存在方法小結(jié)(vc等)
VC判斷進(jìn)程是否存在?比如我想知道記事本是否運(yùn)行,要用到哪些函數(shù)等實(shí)例,需要的朋友可以參考下2013-01-01
C++中Overload,Override,Hide之間的區(qū)別
重載overload,這個(gè)概念是大家熟知的。在同一可訪(fǎng)問(wèn)區(qū)內(nèi)被聲名的幾個(gè)具有不同參數(shù)列的(參數(shù)的類(lèi)型、個(gè)數(shù)、順序不同)同名函數(shù),程序會(huì)根據(jù)不同的參數(shù)列來(lái)確定具體調(diào)用哪個(gè)函數(shù),這種機(jī)制就是重載2013-09-09
基于Sizeof與Strlen的區(qū)別以及聯(lián)系的使用詳解
本篇文章是對(duì)Sizeof與Strlen的區(qū)別以及聯(lián)系的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05
C語(yǔ)言詳細(xì)分析結(jié)構(gòu)體的內(nèi)存對(duì)齊規(guī)則
C 數(shù)組允許定義可存儲(chǔ)相同類(lèi)型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是 C 編程中另一種用戶(hù)自定義的可用的數(shù)據(jù)類(lèi)型,它允許你存儲(chǔ)不同類(lèi)型的數(shù)據(jù)項(xiàng),本篇讓我們來(lái)了解C 的結(jié)構(gòu)體內(nèi)存對(duì)齊2022-07-07
C語(yǔ)言實(shí)現(xiàn)古代時(shí)辰計(jì)時(shí)與現(xiàn)代時(shí)間換算
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)古代時(shí)辰計(jì)時(shí)與現(xiàn)代時(shí)間換算,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03
Matlab實(shí)現(xiàn)帶豎線(xiàn)散點(diǎn)的核密度圖的繪制
核密度估計(jì)是用于估計(jì)隨機(jī)變量概率密度函數(shù)的一種非參數(shù)方法。核密度圖不失為一種用來(lái)觀察連續(xù)型變量分布的有效方法。本文將用Matlab實(shí)現(xiàn)帶豎線(xiàn)散點(diǎn)的核密度圖的繪制,感興趣的可以了解一下2022-08-08
C++11正則表達(dá)式詳解(regex_match、regex_search和regex_replace)
正則表達(dá)式(regular expression)是計(jì)算機(jī)科學(xué)中的一個(gè)概念,又稱(chēng)規(guī)則表達(dá)式,下面這篇文章主要介紹了C++11正則表達(dá)式(regex_match、regex_search和regex_replace)的相關(guān)資料,需要的朋友可以參考下2022-09-09

