当前位置:网站首页>Remove duplicates from an ordered array
Remove duplicates from an ordered array
2022-06-13 00:56:00 【-LM-】
Remove duplicate items from an ordered array
Method 1 ( Two pointer thought )
Use cnt To record the subscript of the new array ,t To record every value added to the new array . First, you need to traverse the original array , The original array was ordered , Therefore, when the number is different from the previous one, it needs to be added to the new array , Then update t. Now t As the latest number to be compared , If the following sum t inequality , Then add it to the new array . The new array here is not redeclared , Instead, it replaces the original array ,cnt It is used to represent the subscript of the array updated each time , from 0 Start .
public static int removeDuplicates(int[] nums) {
if(nums.length==0) return 0;
int len = nums.length;
int cnt=0;
int t=nums[0];
for(int i=0;i<len;i++){
if(nums[i]!=t){
cnt++;
nums[cnt] = nums[i];
t = nums[i];
}
}
return cnt+1;
}
Method 2 ( Double pointer )
Official explanation
Define two pointers \textit{fast}fast and \textit{slow}slow Fast pointer and slow pointer respectively , The fast pointer indicates the subscript position reached by traversing the array , The slow pointer indicates the subscript position to be filled in by the next different element , Initially, both pointers point to the subscript 11.
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
int fast = 1, slow = 1;
while (fast < n) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
}
边栏推荐
- [JS component] calendar
- Pipeline流水线项目构建
- [sca-cnn interpretation] spatial and channel wise attention
- Et5.0 value type generation
- MySQL query table field information
- The grass is bearing seeds
- Arduino uses esp8266+ lighting technology + Xiaoai audio to realize voice control switch
- OceanBase 雄踞墨天轮2021年度中国数据库魔力象限领导者
- How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
- Android Weather
猜你喜欢
随机推荐
什么是 Meebits?一个简短的解释
今日在家休息
Basic operations of FreeMarker
[JS component] dazzle radio box and multi box
Google play console crash information collection
Static analysis of malicious code
kotlin 协程withContext切换线程
Androi天气
With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
Canvas game lower level 100
牌好不好无法预料
sort
Binary tree - right view
MySQL lpad() and rpad() concatenate string functions with specified length
Three column simple Typecho theme lanstar/ Blue Star Typecho theme
[virtual machine] notes on virtual machine environment problems
Influence of higher order poles on waveform
Illustrator tutorial, how to add dashes and arrows in illustrator?
Aunt learning code sequel: ability to sling a large number of programmers
Arduino control tm1637 common positive four digit nixie tube